如何在颤振中没有上下文的情况下导航?

2023-12-05

我最终使用了静态函数,但我需要进行导航,它给了我一个错误,没有找到上下文的吸气剂,所以我寻找解决方案并找到了 GET 包,但当我尝试使用它时,它给了我另一个错误:

E/flutter ( 6078): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] 
Unhandled Exception: NoSuchMethodError: The method 'push' was called on null.

My code:

    void main() {
      runApp(MyApp());
      _MyAppState.autologin();
    }
    
    class _MyAppState extends State<MyApp> {
      static autologin() async {
        var userType;
        var store = Firestore.instance;
        var auth = FirebaseAuth.instance;
        final FirebaseUser user = await auth.currentUser();
        store.collection('Users').document(user.uid).get().then((value) {
          userType = (value.data)['userType'];
          if (userType == 'Student') {
            Get.to(StudentsPage());
          } else if (userType == 'Teacher') {
          } else if (userType == 'Admin') {}
        });
      }

创建导航键

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

将其分配给 MaterialApp

MaterialApp(
    home: Home(),
    navigatorKey: navigatorKey
),

然后通过下面的navigatorKey推送你的路线

navigatorKey.currentState.push(MaterialPageRoute(
    builder: (context) => AnotherPage(),
));

or

navigatorKey.currentState.pushNamed(routeName);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在颤振中没有上下文的情况下导航? 的相关文章

随机推荐