Aman Kataria
Aman Kataria

Reputation: 616

(Flutter) setState of First Page from Second Page

I have a ListView Builder in the first Page, which receives content(items) from the Second Page. When I hit submit button in Second Page, I use:

Navigator.of(context).pop

Which takes me back to First Screen.

The problem is that it is not updating the item created in the list unless i restart the app.

I have tried: 1) Calling an instance of First Page and using:

firstPageinstance.setState((){});

2) I have also tried to call a function from the First Page class, which invokes setState:

firstPageinstance.funtionWhichCallsetStateinFirstageClass();

But none of this works. The list is updated only when I Restart the App.

P.S. The list items are saved and called from an SQLite database. Also, Let me know if you need more details.

Upvotes: 0

Views: 1218

Answers (1)

Figen Güngör
Figen Güngör

Reputation: 12559

When navigating to your SecondPage await the result like this:

bool isUpdated = await Navigator.push(context, MaterialPageRoute(builder: (context) => SecondPage()),)

if(isUpdated) // do your list update logic here

When going back to FirstPage(tap on submit button), send the expected value such as:

Navigator.pop(context, true);

Helpful read: https://flutter.dev/docs/cookbook/navigation/returning-data

Upvotes: 2

Related Questions