Home > Mobile >  Flutter update screen data on resume
Flutter update screen data on resume

Time:07-13

i use the following code to redirect between screens.

Navigator.push(
        context,
        PageRouteBuilder(
          pageBuilder: (context, animation, animation2) => Directionality(
            textDirection: TextDirection.rtl,
            child: screen,
          ),
        ));

now i want it so when the user presses back, the previous screen is displayed and initstate recalled.(in initstate of previous screen i get data from server).

CodePudding user response:

In an async function, you can use await to wait for the user to come back from the pushed screen:

await Navigator.push(...);

But initState is executed only once, so I suggest using setState if you need to rebuild the widget, after the line above:

setState(() {
  // do what you need to update widget
});

so after the above line

CodePudding user response:

initState is called when the state is inited, so it will be called only when the state is created. You could use the then of Navigator.push

Navigator.push(   context,
        PageRouteBuilder(
            pageBuilder: (context, animation, animation2) => Directionality(
                textDirection: TextDirection.rtl,
                child: screen,
        ),
   ).then((_){
        // Your callback here
   });
  • Related