"Navigator operation requested with a context that does not include a Navigator." Error İn Flutter
21.09.2020 18:13
"Navigator operation requested with a context that does not include a Navigator." Error İn Flutter
You wanted to redirect between pages on Flutter, but you encountered such an error. As far as I understand, what is meant by this error is that the sent context does not contain Navigator.
The solution is basically as follows. The class that we will use Navigator should not be the first widget to be called.
There are multiple methods to use to solve this problem. Basically, although the function is the same, the place of use differs. I can list these methods, which are up to you, as follows.
1. We wrap the class we call in runApp in Main with MaterialApp.
void main() {
runApp(MaterialApp(home: test1()));
}
2. The corresponding class is called directly in runApp. Then the class in which the application functions will be made is called as a separate class.
void main() {
setupLocator();
runApp(test1());
}
class test1 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: test3(),
);
}
}
class test3 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: RaisedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => test2(),
),
);
},
child: Text("Tıkla"),
),
),
);
}
}
class test2 extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Text("SAYFA 2"),
),
),
);
}
}