Flutter : Pressing back button closes two screens
So as discussed in the comments. using Navigator.of(context,rootNavigator: true).push(...)
fixed the issue.
But why did you faced this issue at first ? because you have multiple MaterialApp
in your app. you have to keep only the one in main.dart
as root widget. so you have two options:
- Use only one
MaterialApp
as root widget and callNavigator.of(context).push(...)
- Have multiple
MaterialApp
and useNavigator.of(context,rootNavigator: true).push(...)
If you need my advice, use 1.
Sivaram
Updated on December 25, 2022Comments
-
Sivaram over 1 year
I have navigate from Dashboard to Some Screen called 'A'. In that screen, I have shown a listing using ListView Builder. In that, clicking on item in ListView navigate to another Screen called "B". When I press back button in that Screen "B" its navigate to Dashboard. But it has to navigate to Screen "A". Please help me to resolve this issue.
In Dashboard, I have use below code to Navigate to Screen "A",
_showSnackBar(BuildContext context, Item item) { switch(item.name) { case "Disputes": Navigator.push(context, MaterialPageRoute(builder: (context)=> Disputes())); break; } } }
In Screen "A", I have use below code to Navigate to Screen "B",
Card( child: ListTile( onTap: () { Navigator.push( context, MaterialPageRoute( builder: (context) => SubmitDisputes(disputesId: disputeResList[index].id.toString()))); }, trailing: Icon( Icons.remove_red_eye), )); }), ), ),
When I press back button in Screen "B", its navigate to Dashboard. But I need to navigate to "A" as per the backstacks.
Please help me!
-
ikerfah over 3 yearsSeems all good, did you tried to stop the app and restart it again (not hot reload) ? and are you calling
.pop()
somewhere in the code ? -
Sivaram over 3 yearsNope Not. I add ``` .pop() ``` only in app bar action. Its happening when I pressed android system back button or IOS swipe back. I restarted and created a release apk too. But not working
-
ikerfah over 3 yearsCan you please post all the source code for dashboard , Lease and SubmitDisputes ?
-
Sivaram over 3 years@ikerfah I edited the post with source code. Kindly look into that
-
ikerfah over 3 yearsI need to see your main.dart too please
-
Sivaram over 3 yearsI added main.dart
-
Sivaram over 3 yearsI added HomePage too. Hope I show all the code. Kindly help me to resolve this issues.
-
ikerfah over 3 yearsCould please use
rootNavigator: true
as second parameter whenever you are using Navigator ?Navigator.of(context,rootNavigator: true).push(...)
-
Sivaram over 3 yearsWhere I have to add this. In Homepage or Dispute Page?
-
ikerfah over 3 yearsEverywhere :D , Glad it worked. i'll add it as an answer so you can mark it as accepted
-