2

When I leave a page widget and come back to it later, it retains the previous info on the previous session. I would like to create a fresh page widget from scratch every time I opened it. How do I implement this?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
0xba1
  • 76
  • 1
  • 7
  • You can pop navigation twice `Navigator.pop(context)` and then navigate to your screen again, you will find it as a fresh one absolutely. – Shady Boshra Feb 28 '21 at 09:07

3 Answers3

1

I am guessing that your issue is the after going to new page using Navigator.push(), you get the previous state of the page after popping the new page.

In that case you can call a setState after popping the new page. Do the following :

Navigator.pop() on Page2 :

Navigator.pop(context,1);    /// Passing an extra parameter (1) to Page1 after popping

Navigator.push() in Page1 :

Navigator.push(context,MaterialPageRoute(builder: (context) => Page2())).then((value){
      if(value == 1)
        setState(() {});         /// Here you can keep a conditional refresh like if you pass 1 in Navigator.pop() then only will it refresh the page
/// It's useful when you want to refresh the page only for specific conditions.
});

The .then() function after Navigator.push() will be called after Page2 is popped and as mentioned in that function, a setState() will be called refreshing the page.

Sarvesh Dalvi
  • 2,260
  • 2
  • 14
  • 30
1

Are you sure that when you are navigating through the pages you close them properly (end their life circle). If not try to navigate with Are you sure that when you are navigating through the pages you close them properly (end their life circle). If not try to navigate with pushReplacement instead of push

Jordan Kotiadis
  • 558
  • 6
  • 21
0

Putting the main data of the widget in the widget declaration works. I suppose the problem was the scope of the data used. If outside the widget declaration, the data would basically be global and would be the same every time the widget is navigated to.

0xba1
  • 76
  • 1
  • 7