1, problem,
A page jump with Flutter is displayed with the following error
Navigator operation requested with a context that does not include a Navigator.
Copy the code
2. My code
void main() { runApp(MyApp1()); } class MyApp1 extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'open url', home: Scaffold( appBar: AppBar( title: Text('hello flutter'), ), body: Column( mainAxisAlignment: MainAxisAlignment.center, mainAxisSize: MainAxisSize.min, children: <Widget>[ FlatButton( child: Text("go to new page"), textColor: Colors.blue, onPressed: () { Navigator.push(context, MaterialPageRoute( builder:(context) => NewPage())); }, ), ], ), ), ); } } class NewPage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("hello word"), ), body: Center( child: Text("this is new page"), ), ); }}Copy the code
3, why
Navigator operation requested with a context that does not include a Navigator.
Copy the code
The context is inconsistent. Let’s look at the inheritance of the Navigator
class Navigator extends StatefulWidget {
}
Copy the code
But my code looks like this
class MyApp1 extends StatelessWidget {
}
Copy the code
We need to use the Context of the StatefulWidget
4. Solutions
void main() {
runApp(MaterialApp(
title: "Navigation basics",
home: MyApp1(),
));
}
Copy the code
Use MaterialApp start
class MaterialApp extends StatefulWidget {
***
}
Copy the code