我有一个基于文件夹中文件数填充的列表。在应用程序启动时,它可以按照预期正确地建立我的列表。
但是在新笔记屏幕上,用户可以选择将笔记保存在此文件夹中。当我回到此屏幕时,列表不会基于添加的新文件而重建。当我关闭应用程序并重新启动时,该文件将显示在列表中。
遵循以下答案: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'),
),
);
},
);
}
}
答案 0 :(得分:1)
完成此操作的一种方法是将函数传递给 NoteEditScreen(),该函数将在主屏幕中触发回调以重建它(我们已经可以使用 buildTxtList 功能或仅为此创建一个新的
void buildTxtList() {
txtList = easyDir
.listSync()
.map((item) => item.path)
.where((item) => item.endsWith(".txt"))
.toList();
setState(() {}); // To rebuild the Widget
}
然后将其传递下去
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>
NoteEditScreen(onPressed: buildTxtList),
),
);
然后在 NoteEditScreen 中首先声明该属性,最后在 AppBar 中对其进行调用。
注意:您不必在AppBar的 Leading 属性中调用该函数,也可以在将注释保存到文件夹中之后调用它。
final Function onPressed;
NoteEditScreen({this.onPressed});
appBar: AppBar(
title: Text("Screen 2"),
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: () {
onPressed();
Navigator.pop(context);
}
)
),
注意:您应该一定看看这两个有关使用setState(() {})
的帖子
Empty set state what is the point?