0

Giving that I have declared my routes in MaterialApp of my flutter application, now I am using

Navigator.pushNamed(context,ScreenA);

now on some user event I need to open ScreenA but only if ScreenA is not there already otherwise just update arguments in that ScreenA.

2 Answers2

0

Have a look at this. You can await a result from all the pages you open from Screen A and use the values returned from these pages in Screen A once you pop back to it

Sidak
  • 1,213
  • 6
  • 11
  • Actually use case is ScreenA is a screen main screen which has materialApp now screenB got open due to user button click and then via deepLink screenA tries to open ScreenB again, hence this logic will not help me. WDYT? – Jimit Shah May 14 '20 at 10:35
0

You can check the current top screen and set your condition like below,

final newRouteName = "/NewRoute";       // Here add your route name 
bool isNewRouteSameAsCurrent = false;

Navigator.popUntil(context, (route) {
  if (route.settings.name == newRouteName) {
    isNewRouteSameAsCurrent = true;
  }
  return true;
});

if (!isNewRouteSameAsCurrent) {
  Navigator.pushNamed(context, newRouteName);
}

Refer.

Kaushik Bhingradiya
  • 827
  • 1
  • 13
  • 23
  • Actually use case is ScreenA is a screen main screen which has materialApp now screenB got open due to user button click and then via deepLink screenA tries to open ScreenB again, hence this logic will not help me. Aprt from this above one will be fired from context of screenA hence it will clear ScreenA and not ScreenB – Jimit Shah May 14 '20 at 10:35
  • This makes sense when you are routing by using same screen context else it will show the name of the screen from which context is used – Jimit Shah May 19 '20 at 07:47