gpt4 book ai didi

flutter - 为什么用这个简单的initState代码使flutter崩溃?

转载 作者:行者123 更新时间:2023-12-03 04:42:54 26 4
gpt4 key购买 nike

class _HomeScreenState extends State<HomeScreen> {
String uid = "";

@override
void initState() {
FirebaseAuth.instance.currentUser().then((user) {
setState(() {
uid = user.uid;
super.initState();
});
});
}
我运行的是最新版本的Flutter,以前从未遇到过这些问题。 Flutter完成了xcode的构建,在模拟器上打开,但只有白色的屏幕,并且在1秒后Flutter崩溃,出现以下异常:
**Failed assertion: line 1110 pos 12: '_debugLifecycleState == _StateLifecycle.created': is not true.
#0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:42:39)
#1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:38:5)
#2 State.initState (package:flutter/src/widgets/framework.dart:1110:12)
#3 _HomeScreenState.initState.<anonymous closure>.<anonymous closure> (package:schoolEachother1_0/screens/home_screen.dart:25:15)
#4 State.setState (package:flutter/src/widgets/framework.dart:1233:30)
#5 _HomeScreenState.initState.<anonymous closure> (package:schoolEachother1_0/screens/home_screen.dart:23:7)
#6 _rootRunUnary (dart:async/zone.dart:1192:38)
#7 _CustomZone.runUnary (dart:async/zone.dart:1085:19)
#8 _FutureListener.handleValue (dart:async/future_impl.dart:141:18)*
#9 Future._propagateToListeners.handleValueCallback (dart:async/futu<…>
*** First throw call stack:
(
0 CoreFoundation 0x00007fff23e3cf0e __exceptionPreprocess + 350
1 libobjc.A.dylib 0x00007fff50ba89b2 objc_exception_throw + 48
2 CoreFoundation 0x00007fff23e3cc88 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff258b8c9b -[NSAssertionHandler handleFailureInFunction:file:lineNumber:description:] + 166
4 Runner 0x00000001031ad6d7 _ZN8firebase9firestore4util16ObjcThrowHandlerENS1_13ExceptionTypeEPKcS4_iRKNSt3__112basic_stringIcNS5_11char_traitsIcEENS5_9allocatorIcEEEE + 343
5 Runner 0x00000001031acf25 _ZN8firebase9firestore4util5ThrowENS1_13ExceptionTypeEPKcS4_iRKNSt3__<…>*
谁能告诉我我做错了什么?
提前致谢

最佳答案

super.initState()必须与initState()方法中的其余代码独立调用。

  @override
void initState() {
FirebaseAuth.instance.currentUser().then((user) {
setState(() {
uid = user.uid;
});
});
super.initState();
}

关于flutter - 为什么用这个简单的initState代码使flutter崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62860096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com