我最终使用了一个静态函数,但是我需要进行导航,这给了我一个错误,即没有为上下文找到getter,所以我寻找了一个解决方案并找到了GET包,但是当我尝试使用它时我另一个错误:
E/flutter ( 6078): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)]
Unhandled Exception: NoSuchMethodError: The method 'push' was called on null.
我的代码:
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') {}
});
}
答案 0 :(得分:2)
创建导航键
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
将其分配给MaterialApp
MaterialApp(
home: Home(),
navigatorKey: navigatorKey
),
然后通过下面的navigatorKey推送路线
navigatorKey.currentState.push(MaterialPageRoute(
builder: (context) => AnotherPage(),
));
或
navigatorKey.currentState.pushNamed(routeName);