Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call function to update value after poping screen in flutter?

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?

like image 574
PrakashKing Avatar asked May 06 '20 19:05

PrakashKing


People also ask

How do you pass data when popping in flutter?

Flutter: Passing Data when Popping Views in Flutter You can replace the code in _MyHomePageState with these: The main magic is in moveToSecondPage where it is an async function, awaiting for the SecondPage to come back with some “data”, which we assign it to information here.

How do you call a function automatically in flutter?

You should make the call of your function in initState rather then in widget tree as everytime the widget is rebuild, it will call the function to execute again and again(leading to more reads).


2 Answers

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.
  });
like image 58
Csaba Farkas Avatar answered Nov 08 '22 10:11

Csaba Farkas


You can use Provider or BLoc or you can await for the result when you push the page

like image 33
Omar Elhamy Avatar answered Nov 08 '22 10:11

Omar Elhamy