如何更新位于前一屏幕的列表视图的状态
How to update state of a listview located in the previous screen
我目前正在开发一个带有 flutter 的 sqflite 应用程序,并尝试在从另一个屏幕执行 navigator.pop 操作后设置我的列表状态,但它不会加载新状态,除非我进行 [=16] =] 我的代码片段是 below.Also,如果有帮助,我可以分享整个代码。如何在不重启我的应用程序的情况下设置 listView 的状态?
// these methods are on my first screen that I display my listview
void getNoteList() {
final notesFutureList = db.getNotes();
notesFutureList.then((data) {
setState(() {
notes = data;
});
});
}
@override
void initState() {
getNoteList();
}
// This method is on my adding screen attached to the button which performs the saving.
Future<void> saveNote(BuildContext context) async {
await db.insertNote(Note(
header: myControllerHeader.text,
detail: myControllerDetail.text)); // inserted to db.
Navigator.pop(context);
// Navigator.push(context, MaterialPageRoute(builder: (context) => const NoteListScreen(),));
// if a use another push like this it's working. But doesn't look like a good way.
}
您可以从您说 Navigator.push() 的地方将函数转换为异步。等待route的return,然后setState。就像
navigateToDataEntryScreen()async{
await Navigator.pushNamed("DataAddingScreen");
setState((){});
}
或者
navigateToDataEntryScreen()async{
await Navigator.pushNamed("DataAddingScreen");
getNoteList();
}
我目前正在开发一个带有 flutter 的 sqflite 应用程序,并尝试在从另一个屏幕执行 navigator.pop 操作后设置我的列表状态,但它不会加载新状态,除非我进行 [=16] =] 我的代码片段是 below.Also,如果有帮助,我可以分享整个代码。如何在不重启我的应用程序的情况下设置 listView 的状态?
// these methods are on my first screen that I display my listview
void getNoteList() {
final notesFutureList = db.getNotes();
notesFutureList.then((data) {
setState(() {
notes = data;
});
});
}
@override
void initState() {
getNoteList();
}
// This method is on my adding screen attached to the button which performs the saving.
Future<void> saveNote(BuildContext context) async {
await db.insertNote(Note(
header: myControllerHeader.text,
detail: myControllerDetail.text)); // inserted to db.
Navigator.pop(context);
// Navigator.push(context, MaterialPageRoute(builder: (context) => const NoteListScreen(),));
// if a use another push like this it's working. But doesn't look like a good way.
}
您可以从您说 Navigator.push() 的地方将函数转换为异步。等待route的return,然后setState。就像
navigateToDataEntryScreen()async{
await Navigator.pushNamed("DataAddingScreen");
setState((){});
}
或者
navigateToDataEntryScreen()async{
await Navigator.pushNamed("DataAddingScreen");
getNoteList();
}