0

I declared a class, returned MaterialApp and used button in it also used Navigator.Push method for navigation to different page, but it gave exception

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
            SafeArea(
            child: Scaffold(
            backgroundColor: Colors.red[100],
            body:Column(
              children: [
                   SizedBox(height: 50,),
                  Align(
                    alignment:Alignment.center,
                     child:Image.asset("assets/icons/appicon.png",
                   height:150),
                   ),
                   
                   Align(
                     alignment: Alignment.bottomCenter,
                     child: RaisedButton(  
  
                             child: Text('Click Picture'),  
  
                             color: Colors.red[800],  
  
                               onPressed: () {
  
                                            Navigator.push(
  
                                            context,
  
                                            MaterialPageRoute(builder: (context) =>  CameraRoute()
                                            ),
                                            );
  
                               },  
  
                             
  
                     ),
                   )
              ],
             
          ),
              
            ),
                        
        )
);
  

    //throw UnimplementedError();
  }
}
June7
  • 19,874
  • 8
  • 24
  • 34
Mansi Mandlik
  • 120
  • 11
  • Hey @Masi Mandlik. Please go through this https://stackoverflow.com/help/how-to-ask to ask questions better. – Josteve Jan 14 '21 at 08:54
  • Does this answer your question? [Navigator operation requested with a context that does not include a Navigator](https://stackoverflow.com/questions/44004451/navigator-operation-requested-with-a-context-that-does-not-include-a-navigator) –  Jan 14 '21 at 08:55
  • 1
    Please don't post images of code or error messages. Paste them here and format them appropriately. –  Jan 14 '21 at 10:24
  • Sorry, It was my 1st time to ask question here. so I was in hurry to post it that's why I didn't understand how to format it properly – Mansi Mandlik Jan 14 '21 at 18:45
  • hey @Dude, Thanks it was helpful – Mansi Mandlik Jan 14 '21 at 18:46

2 Answers2

0

Separate the page/screen from MyApp by creating a new widget.

Like so

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Center(
          child: Column(
            children: [
              SizedBox(height: 100),
              Image.asset(
                "assets/icons.appicon.png",
                height: 150,
              ),
              RaisedButton(
                child: Text("Click Picture"),
                color: Colors.red,
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => CameraRoute(),
                    ),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
Josteve
  • 11,459
  • 1
  • 23
  • 35
0

You're trying to get Navigator from a context that doesn't have a Navigator as parent. The only navigator in your code is in the MaterialApp, but that is a child of context.

The minimal way of getting a context that is a child of MaterialApp is to wrap your Scaffold with a Builder.

A better approach would be to split your code into more widgets (the build-method of each widget exposes a context) as mentioned by the other answer.

spkersten
  • 2,849
  • 21
  • 20