0

i have a showSnackbar method that looks like this in my class provider class:

  GlobalKey<ScaffoldState> scaffoldKey =
      GlobalKey<ScaffoldState>(debugLabel: 'scaffoldKey');

  void showSnackBarE(String label) {
    if (purchasedItems[label] != 0) {
      final snackBar = SnackBar(
        content: Text("$label has already been added to cart!"),
        backgroundColor: Colors.black87,
        behavior: SnackBarBehavior.fixed,
        duration: Duration(seconds: 1),
      );
      scaffoldKey.currentState.removeCurrentSnackBar();
      scaffoldKey.currentState.showSnackBar(snackBar);
    } else {
      final snackBar = SnackBar(
        action: SnackBarAction(
            label: "Undo",
            onPressed: () {
              purchasedItems[label] = 0;
              getTotalSum();
            }),
        content: Text("$label has been added to cart!"),
        backgroundColor: Colors.black87,
        behavior: SnackBarBehavior.fixed,
        duration: Duration(seconds: 1),
      );
      scaffoldKey.currentState.removeCurrentSnackBar();
      scaffoldKey.currentState.showSnackBar(snackBar);
    }

    notifyListeners();
  }

at my TabsScreen im giving the scaffold the same key i used from provider

scaffold(
key: mainProvider.scaffoldkey,
..
...

every tab i have uses the same widget in which when the widget is pressed this snackbar will be called..

if i try to navigate back to the tabs screen like this:

                    Navigator.of(context)
                        .pushReplacementNamed(TabsScreen.id);

from a screen inside a screen from the tab bar appbar it gives me this error.. what should i do?

azheen
  • 897
  • 4
  • 15
  • 30

1 Answers1

1

As docs say to go back from the screen inside you should use Navigator.pop(context);

Edit: Ok so it seems that in this case the best solution is to use the Navigator.popUntil(context, ModalRoute.withName('screen_route')); function.

  • im navigating to a page from tabb screen's appbar then from there i go to the page i Navigate back to the tab screen from. so navigator.pop() will make me go back to the page i opened from the app bar and not to the tabs screen itself – azheen Jul 01 '20 at 12:26
  • I see, can you navigate to TabsScreen from the page you opened the app bar? If yes then the workaround I can think of is to use Navigator.pop() and in the page you will get to in initState() navigate to your desired location. You can set some bool to determine if you just want to pass through or stay on this page. Let me know if I explained it clear enough... – David Czuchnowski Jul 01 '20 at 12:33
  • thankyou, gives me the same error if i navigate to tabs screen in the page i opened at the tab screens appbar – azheen Jul 01 '20 at 12:38
  • then please show what your String id looks like for every page – David Czuchnowski Jul 01 '20 at 12:42
  • using a bottom navigation bar: static const String id = '/tabs screen'; then from tabscreen appbar i go to cart screen: static const id = '/Cart Screen'; then from cart screen i go to submit order screen, the sumbit screen doesnt have an id because im passing things to it so in the navigator i dont use a named route i use the normal routing of navigator.push(.... – azheen Jul 01 '20 at 12:46
  • ok then if you are using "/something" as name in your Strings id then homePage or initialRoute HAS TO BE NAMED "/" only "/" nothing else – David Czuchnowski Jul 01 '20 at 12:50
  • initial route is '/' and its a loading screen that loads for 3 seconds then goes to tabsscreen, i tried navigating to the initialRoute which is '/' but it gave me the same error as well – azheen Jul 01 '20 at 12:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217018/discussion-between-david-czuchnowski-and-azheen). – David Czuchnowski Jul 01 '20 at 13:23