0

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).

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56

3 Answers3

1

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

Peter Koltai
  • 8,296
  • 2
  • 10
  • 20
1

initState is called when the state is initialized, 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
  })
)
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
VincentDR
  • 696
  • 3
  • 15
0

First, you need to create another function inside your first screen and get the data inside of it. then you can call this function both inside the initState and after awaiting for the push command to execute.