10

I am trying to run some method when user click back or I use Navigator.pop() in second screen.

I have two screens.

  1. Setting screen
  2. Edit screen

After editing I pop user to first second. Now in first screen I use SharedPreference. But I need to re run a method when user comes back to 1st screen. How can I do this?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Hkm Sadek
  • 2,987
  • 9
  • 43
  • 95

1 Answers1

12

While navigating to Edit screen from Settings screen, use this (inside your Settings screen)

Navigator.pushNamed(context, "/editScreen").then((_) {
  // you have come back to your Settings screen 
  yourSharedPreferenceCode();
});

If you only want to run this code on some event that happened on your Edit screen, you can make use of pop method inside Edit screen like

Navigator.pop(context, true); // pass true value

And above code should be:

Navigator.pushNamed(context, "/editScreen").then((value) {
  if (value) // if true and you have come back to your Settings screen 
    yourSharedPreferenceCode();
});

Edit:

async-await would look something like:

bool value = await Navigator.pushNamed(context, "/editScreen");
if (value) // if true and you have come back to your Settings screen 
  yourSharedPreferenceCode();
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440