gpt4 book ai didi

android - 从文件中读取值并放入Flutter中的ListView

转载 作者:行者123 更新时间:2023-12-03 03:56:03 25 4
gpt4 key购买 nike

我正在尝试从文本文件中读取逗号分隔的值,用逗号分隔字符串,然后将这些值中的每一个显示为ListTile中的ListView

我真的不知道如何正确地做到这一点。这个异步代码正在杀死我!

tiles.txt

first,second,third,fourth,fifth

这是没有从文本文件中读取任何内容的代码:

main.dart
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text("My App"),
),
body: MainPage(),
),
);
}
}

class MainPage extends StatelessWidget {
final listItems = [];

@override
Widget build(BuildContext context){
return ListView.separated(
itemCount: listItems.length,
itemBuilder: (context, index){
return ListTile(
leading: Icon(Icons.invert_colors),
title: Text(listItems[index]),
);
},
separatorBuilder: (context, index){
return Divider();
},
);
}
}

如果有人能弄清楚如何从文件中读取并将其显示为 ListView,那将是很好的,即使这不是我编写的方式!

最佳答案

您可以在下面复制粘贴运行完整代码
您可以使用https://pub.dev/packages/csv软件包
步骤1:以读取模式打开文件

final input = new File('$appDocPath/tiles.txt').openRead();

步骤2:使用 transformCsvToListConverter
listItems = await input
.transform(utf8.decoder)
.transform(new CsvToListConverter())
.toList();

假设您在 tile.text目录中有一个 /data/user/0/yourDomain.yourProjectname/app_flutter
enter image description here

程式码片段
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
print(appDocPath);
final input = new File('$appDocPath/tiles.txt').openRead();
listItems = await input
.transform(utf8.decoder)
.transform(new CsvToListConverter())
.toList();

...
ListView.separated(
itemCount: listItems[0].length,
itemBuilder: (context, index) {
return ListTile(
leading: Icon(Icons.invert_colors),
title: Text(listItems[0][index]),
);

工作演示

enter image description here

完整的代码
import 'package:flutter/material.dart';
import 'package:csv/csv.dart';
import 'dart:io';
import 'dart:convert';
import 'package:path_provider/path_provider.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
dynamic listItems;

void _incrementCounter() async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
print(appDocPath);
final input = new File('$appDocPath/tiles.txt').openRead();
listItems = await input
.transform(utf8.decoder)
.transform(new CsvToListConverter())
.toList();
print(listItems[0][0].toString());
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
listItems == null
? Container()
: Expanded(
child: ListView.separated(
itemCount: listItems[0].length,
itemBuilder: (context, index) {
return ListTile(
leading: Icon(Icons.invert_colors),
title: Text(listItems[0][index]),
);
},
separatorBuilder: (context, index) {
return Divider();
},
),
),
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

关于android - 从文件中读取值并放入Flutter中的ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61235247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com