Reputation: 37
I have this main dart file
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
MainController().init().then((value) {
if(Version().canUpdate()) {
Navigator.pushReplacementNamed(context, '/update');
}
MainController().getAccountInfos((datas) {
if(datas["auth"]) {
Navigator.pushReplacementNamed(context, '/');
}
else {
Navigator.pushReplacementNamed(context, '/login');
}
});
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: new ThemeData(
scaffoldBackgroundColor: ColorPalette.primary
),
initialRoute: "/loading",
routes: {
'/': (context) => MainMenuWidget(),
'/login': (context) => LoginWidget(),
'/register': (context) => RegisterWidget(),
'/keys': (context) => KeysPageWidget(),
'/update': (context) => UpdatePageWidget(),
'/loading': (context) => LoadingPageWidget(),
},
);
}
}
and I try to display a loading view during app init before change page.
Actually, I have a "Navigator operation requested with a context that does not include a Navigator." error.
I need help please.
Upvotes: 0
Views: 262
Reputation: 5333
You are using the Navigator
logic inside _MyAppState
, initState()
method - here, your context does not have a defined Navigator, that's basically what the error explains. Navigator context could be accessed only under the MaterialApp()
(since you define your routes, navigation logic only there).
You can resolve this by moving all the initState()
logic to the LoadingPageWidget
- there you could access the Navigator and since it's your default first page, there should be no side effects behind this solution.
Upvotes: 1