The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget

Viewed 811

I'm trying to navigate to another screen from the first one, so basically, I'm checking if the user is logged in or not and based on that I navigate or not to the next screen, but I'm getting an error of a context while trying that

This is my code

Future _AuthenticateUser(BuildContext context){
  FirebaseAuth.instance
      .authStateChanges()
      .listen((event) {
      if(event == null){
      // User is null
     } else {

        // here I navigate to the next screen
        Navigator.push(context,
            MaterialPageRoute(builder: (context) => new MainScreen()));

    }
  });
}

I'm calling the code at first to check for user logging state

 @override
  void didChangeDependencies() {
    _AuthenticateUser(context);
    super.didChangeDependencies();

  }

This is the error i'm getting

The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
1 Answers

First lets know why we get this error..

static Future<T> push<T extends Object>(BuildContext context, Route<T> route) {
  return Navigator.of(context).push(route);
}

the above push function "push" search for the navigator in the context and push your screen on it so what i can conclude is that you need to check if the parents have navigator or not....

Related