How to call a function to update a value after popping a screen in Flutter?

0

Issue

Screen 1: shows list of item with add button.
Screen 2: form to add a new item to the list.

Screen 2 >> Screen 1 – While calling navigator.pop() in screen 2, how to call method/setState (to update list) in screen 1?
Can anyone help me out?

I don’t want to relaunch screen again. I just need to run a method to update my list after popping previous screen?

Solution

When you navigate from Screen 1 to Screen 2, you use the push method. The push method returns a Future object.
You can use the then method of Future to execute your code after Screen 2 was popped from the navigation stack.

Navigator.of(context)
  .push(MaterialPageRoute(
     builder: (context) => Screen2(),
  ))
  .then((value) {
    // you can do what you need here
    // setState etc.
  });

Answered By – Csaba Farkas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More