Diljith
Diljith

Reputation: 69

How to update state when Navigating back to a page Flutter

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,

Upvotes: 0

Views: 1330

Answers (2)

Omar Abed
Omar Abed

Reputation: 36

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.

Upvotes: 1

CCP
CCP

Reputation: 1236

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

Upvotes: 1

Related Questions