Home > Blockchain >  How to update state when Navigating back to a page Flutter
How to update state when Navigating back to a page Flutter

Time:01-11

i am creating a social media like app, when user clicks on post ,it redirects to a commentpage,in commentspage data is coming from a seperate API,so when go back using Navigator.pop(context), the data in home is not updated, if pressed like button,its not reflecting in ui, but updating in Api,

i wrapped Navigator with setstate but its not reflecting,

CodePudding user response:

Use this on navigator push Navigator.push(context).then((value)=>setState((){}); when it pop the screen it will run the setState

CodePudding user response:

Use this sentence to go to the second page so the function will wait second screen to close

bool result = await Navigator.push(context);
if(result){setState((){});} 

when u close the second screen use this sentence Navigator.pop(context, true);

so when u comment has been posted successfully, u will back to screen one and complete the function if result boolean is true, else do nothing.

CodePudding user response:

You need to await for Navigator and call setState after that like this:

await Navigator.pushNamed(context,"next screen name");
setState(() {});

Note: it doesn't make any difference how you go to next page as long as it wasn't replace one.

  • Related