如何在小部件initState()中获取导航器的上下文?

时间:2019-03-01 12:30:56

标签: flutter

我希望我的应用无需用户设置即可脱机工作,并在连接恢复时要求登录

我正在尝试的代码的摘录:

class _MyAppState extends State<MyApp> {

  @override
  void initState() {
    super.initState();

    Connectivity().onConnectivityChanged.listen((ConnectivityResult result) =>
        checkConnectivity().then((isOnline) {
          if (isOnline && MyApp.store.state.user == null)
              Navigator.of(context).pushReplacement(
                MaterialPageRoute(builder: (context) => LoginPage()),
              );
        }));
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(          
      routes: {
...

但是我能得到的只是这个错误:

Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.

我试图按照here的描述将Navigator调用包装在Future.delayed内,但是出现了相同的错误

0 个答案:

没有答案