0

For ex.

I am on Screen-1 , push screen and goto Screen-2.
and then I replace Screen-2 with Screen-3
and now I pop that Screen-3 with pass some data
and come back to Screen-1 and I want refresh data in Screen-1

enter image description here

Sanjayrajsinh
  • 15,014
  • 7
  • 73
  • 78

1 Answers1

0

You can use something like this (but you must go from 3 to 2 and finally 1) to get some value then refresh it:

var result = await Navigator.of(context).push(
            MaterialPageRoute(builder: (context) => Screen2()));
        if (result != null) {
          setState(() {
            valueYouChangeIt = result;
          });
        }

And then send this value back from Screen2 with:

_sendDataBack(BuildContext context) {
    Navigator.pop(context, valueToSendBack);
  }

Or if you want to refresh everything you can write _initState function where you initialize your data and then call _initState on result.

Said that, all this can be super tedious process, so if it's a small app with 2 to 5 screens it can be ok, but with more screens, like someone else commented, I'd prefer to use Provider.

i6x86
  • 1,557
  • 6
  • 23
  • 42
  • This would make your UX look ugly. – CopsOnRoad Mar 25 '20 at 18:44
  • well I'm not recommending it but I have an app in production that use this approach and it works just fine, but I'm refactoring everything implementing provider, because there is too much code there and is too dificult to read even for me... – i6x86 Mar 25 '20 at 18:52