Exception : Navigator operation requested with a context that does not include a Navigator

146

You can wrap your Scaffold with a Builder widget and it will provide you a context.

Here is the updated code

@override
Widget build(BuildContext context) {
SystemChrome.setEnabledSystemUIOverlays([]);

return MaterialApp(
  routes: <String, WidgetBuilder>{
    '/homepage': (context) => MyApp(),
    '/loginpage': (BuildContext context) => new LoginPage()
  },
  home: Builder(
    builder:(context)=>Scaffold (
      body: Center(
        child: GestureDetector(
          onTap: () {
            Navigator.of(context).pushNamed('/loginpage');
          },
          child: Image(
            width: 100.0,
            height: 100.0,
            image: AssetImage('assets/images/icon.png'),
          ),
        ),
       ),
      ),
     ),
   );
 }

Hope this helps!

Share:
146
Lahiru Liyanage
Author by

Lahiru Liyanage

Updated on December 20, 2022

Comments

  • Lahiru Liyanage
    Lahiru Liyanage over 1 year

    This error has a lot of solutions. But I am getting this error. I tried several ways at this site. but could not get a solution. This is my code

    @override
    Widget build(BuildContext context) {
    
     // TODO: implement build
     SystemChrome.setEnabledSystemUIOverlays([]);
    
     return MaterialApp(
      routes: <String, WidgetBuilder>{
        '/homepage': (context) => MyApp(),
        '/loginpage': (BuildContext context) => new LoginPage()
      },
      home: Scaffold (
        body: Center(
          child: GestureDetector(
            onTap: () {
    
            Navigator.of(context).pushNamed('/loginpage');
            },
            child: Image(
              width: 100.0,
              height: 100.0,
              image: AssetImage('assets/images/icon.png'),
            ),
          )
    
        ),
    
      ),
     );
    }