Home > Software engineering >  How to update state of a listview located in the previous screen
How to update state of a listview located in the previous screen

Time:04-22

I am currently developing an sqflite application with flutter and trying to set state of my list after performing a navigator.pop operation from another screen but it doesn't load the new state unless I make a hotrestart.Useful snippets from my code are below.Also, I can share whole code if it helps. How can I set state of listView without restarting my app?

// 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.
  }

CodePudding user response:

You can turn the function into async from where you are saying Navigator.push(). Wait for the return of route and then setState. Just like

navigateToDataEntryScreen()async{
 await Navigator.pushNamed("DataAddingScreen");
 setState((){});
}

Or

navigateToDataEntryScreen()async{
 await Navigator.pushNamed("DataAddingScreen");
 getNoteList();
}
  • Related