Flutter : finish all the screen/activity (for android/iOS both)

1.6k Views Asked by At

How can I finish all the activity/screen in Flutter.

I want it for Android and iOS.

Is there any way to same like finishAffinity() in Android we do.

Example:

A -> B -> C -> D -> Force Logout -> Open Login Screen and close all previous screen.
3

There are 3 best solutions below

1
BosS On BEST ANSWER

you can use this:

Navigator.pushAndRemoveUntil(context, yourRouteToLogin, (route) => false);

or if you have named routes:

  Navigator.pushNamedAndRemoveUntil(
      context, yourRouteName, (Route<dynamic> route) => false);
0
Viacheslav On

If you are using named routes, you can do this like that: Navigator.of(context).pushNamedAndRemoveUntil('/login', (route) => false);.

0
user3833732 On

For those not using string named routes... The above didnt work for me.

Instead do this....

Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (_) => MyLoginPage()), (route) => false);

I had to do some RnD... but this works fine