如何在获取特定数据后更新 ListView?
How can I update the ListView after getting a certain data?
我遇到过这个问题,
- 我使用这个
_openFile
函数读取文件。阅读后,我想使用提取的数据更新 ListView。但是我找不到在 _openFile
. 执行后重置 ListView 状态的方法
有什么帮助吗?
class ListTest extends StatefulWidget {
@override
_ListTestState createState() => _ListTestState();
}
class _ListTestState extends State<ListTest> {
List<XFile> _files = [];
Future <List<XFile>> _openFile(BuildContext context) async {
final XTypeGroup pngTypeGroup = XTypeGroup(
label: 'PNGs',
extensions: ['png'],
);
_files = await openFiles(acceptedTypeGroups: [
pngTypeGroup,
]);
}
@override
Widget build(BuildContext context) {
return Column(
children: [
RaisedButton(
color: Colors.blue,
textColor: Colors.white,
child: Text('Press to open images (png)'),
onPressed: () {
_openFile(context);
);
}
),
Expanded(
child: ListView.builder(
itemCount: _files.length,
itemBuilder: (context, index) {
return Text(_files[index].path);
},
),
),
],
);
}
}
您需要调用 setState 才能重建小部件。我推荐你阅读 this.
您可以尝试类似的方法:
var _newFiles = await openFiles(acceptedTypeGroups: [
pngTypeGroup,
]);
setState(() {
_files = _newFiles;
});
我遇到过这个问题,
- 我使用这个
_openFile
函数读取文件。阅读后,我想使用提取的数据更新 ListView。但是我找不到在_openFile
. 执行后重置 ListView 状态的方法
有什么帮助吗?
class ListTest extends StatefulWidget {
@override
_ListTestState createState() => _ListTestState();
}
class _ListTestState extends State<ListTest> {
List<XFile> _files = [];
Future <List<XFile>> _openFile(BuildContext context) async {
final XTypeGroup pngTypeGroup = XTypeGroup(
label: 'PNGs',
extensions: ['png'],
);
_files = await openFiles(acceptedTypeGroups: [
pngTypeGroup,
]);
}
@override
Widget build(BuildContext context) {
return Column(
children: [
RaisedButton(
color: Colors.blue,
textColor: Colors.white,
child: Text('Press to open images (png)'),
onPressed: () {
_openFile(context);
);
}
),
Expanded(
child: ListView.builder(
itemCount: _files.length,
itemBuilder: (context, index) {
return Text(_files[index].path);
},
),
),
],
);
}
}
您需要调用 setState 才能重建小部件。我推荐你阅读 this.
您可以尝试类似的方法:
var _newFiles = await openFiles(acceptedTypeGroups: [
pngTypeGroup,
]);
setState(() {
_files = _newFiles;
});