【发布时间】:2026-02-06 22:15:01
【问题描述】:
我有一个根据文件夹中文件数量填充的列表。在应用程序启动时,它会按预期正确地构建我的列表。
但在新笔记屏幕上,用户可以选择在此文件夹中保存笔记。当我回到这个屏幕时,列表不会根据添加的这个新文件重建。当我关闭应用程序并重新启动时,该文件会显示在我的列表中。
按照这个答案:is it possible to refresh or reload page with navigator.pop... like 1st (nav.push=>2) page to 2nd page and back from 2nd (nav.pop, value) to 1st page? 我将.then() 与Navigator.push 一起使用并在setState() 中重建了我的列表,但它仍然没有更新。
我做错了什么?以下是我的代码:
class _HomeListViewState extends State<HomeListView> {
Directory easyDir;
var txtList;
@override
void initState(){
super.initState();
easyDir = Directory(widget.folderPath);
buildTxtList();
}
void buildTxtList(){
//list of paths of my txt files in the folder
txtList = easyDir
.listSync()
.map((item) => item.path)
.where((item) => item.endsWith(".txt"))
.toList();
}
@override
Widget build(BuildContext context) {
print(txtList.length);
return ListView.builder(
itemCount: txtList.length,
padding: EdgeInsets.all(10.0),
itemBuilder: (context, index){
File file = new File(txtList[index]);
String name = file.path.split('/').last;
return Card(
color: (index % 2 == 0) ? Colors.grey.shade300 : Colors.white,
shadowColor: Colors.teal,
elevation: 7.0,
child: ListTile(
leading: Icon(Icons.assignment),
title: Text(name),
trailing: Icon(Icons.keyboard_arrow_right),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => NoteEditScreen(
txtPath: file.path,
),
),
).then((value) {
setState(() {
buildTxtList();
});
});
},
onLongPress: () => print('list tile long pressed'),
),
);
},
);
}
}
【问题讨论】: