0

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.

1
  • Hi There. what issue are u actually facing ? Commented May 22, 2021 at 16:05

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I think I will resolve like this
Great! Do not forget to mark the answer as the correct one if this was helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.