Flutter : Pressing back button closes two screens

498

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:

  1. Use only one MaterialApp as root widget and call Navigator.of(context).push(...)
  2. Have multiple MaterialApp and use Navigator.of(context,rootNavigator: true).push(...)

If you need my advice, use 1.

Share:
498
Sivaram
Author by

Sivaram

Updated on December 25, 2022

Comments

  • Sivaram
    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
      ikerfah over 3 years
      Seems 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
      Sivaram over 3 years
      Nope 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
      ikerfah over 3 years
      Can you please post all the source code for dashboard , Lease and SubmitDisputes ?
    • Sivaram
      Sivaram over 3 years
      @ikerfah I edited the post with source code. Kindly look into that
    • ikerfah
      ikerfah over 3 years
      I need to see your main.dart too please
    • Sivaram
      Sivaram over 3 years
      I added main.dart
    • Sivaram
      Sivaram over 3 years
      I added HomePage too. Hope I show all the code. Kindly help me to resolve this issues.
    • ikerfah
      ikerfah over 3 years
      Could please use rootNavigator: true as second parameter whenever you are using Navigator ? Navigator.of(context,rootNavigator: true).push(...)
    • Sivaram
      Sivaram over 3 years
      Where I have to add this. In Homepage or Dispute Page?
    • ikerfah
      ikerfah over 3 years
      Everywhere :D , Glad it worked. i'll add it as an answer so you can mark it as accepted