Farhana Naaz Ansari
Farhana Naaz Ansari

Reputation: 7944

Flutter: Navigation issue

I'm not able to navigate screen showing error 'Navigator operation requested with a context that does not include a Navigator, I have tried many solutions where navigator is used in Builder with stateless widgets but here navigation is done automatically after a few seconds in override method in intiSate. my aim is to navigate the screen after a few seconds.

class Splash extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return testWidget;
      }
    }

Widget testWidget = new MediaQuery(
    data: new MediaQueryData(),
    child: new MaterialApp( title: 'xxxxxxxxxxxxx',
      home: SplashScreen(),
      debugShowCheckedModeBanner: false,
      routes: <String, WidgetBuilder>{
        '/login': (BuildContext context) => new Login(),
      },
    )

);

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => new _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen>  {
  @override
  Future initState ()  {
    super.initState();
   new Future.delayed(
        const Duration(seconds: 2), () => Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => Login()),
        ));
  }
  @override
  Widget build(BuildContext context) {

    return(Scaffold(
      body: Container(
        height: double.infinity,
        width: double.infinity,
        child: Image.asset('assets/images/crop.jpg',fit:BoxFit.fill),
      ),
    ));
    //build
  }
}

Showing Error

Navigator operation requested with a context that does not include a Navigator. 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.

Upvotes: 0

Views: 703

Answers (1)

anmol.majhail
anmol.majhail

Reputation: 51176

Code Corrected:

MaterialApp Should always be the Root Widget of all Widgets. That Way Navigator is always Available.

void main() => runApp(Splash());

class Splash extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: testWidget,
      debugShowCheckedModeBanner: false,
    );
  }
}

Widget testWidget =
    new MediaQuery(data: new MediaQueryData(), child: new SplashScreen());

class SplashScreen extends StatefulWidget {
  @override
  _SplashScreenState createState() => new _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {
  @override
  void initState() {
    super.initState();
    Future.delayed(
        const Duration(seconds: 2),
        () => Navigator.pushReplacement(
              context,
              MaterialPageRoute(builder: (context) => Login()),
            ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Spalsh'),),
      body: Container(
        height: double.infinity,
        width: double.infinity,
//        child: Image.asset('assets/images/crop.jpg', fit: BoxFit.fill),
      ),
    );
    //build
  }
}

class Login extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Login Page'),
      ),
      body: Container(),
    );
  }
}

Upvotes: 2

Related Questions