gpt4 book ai didi

android - 抖动错误 LateInitializationError : Field '@' has not been initialized

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

这是预计会导致错误的主程序的中间部分。如果您未在启动屏幕上登录,此代码将转到 MyHomePage 并登录。如果您已登录,它将转到 MainScreen 并切换到应用程序的主屏幕。

  class SplashScreen extends StatefulWidget {
@override
_SplashScreenState createState() => _SplashScreenState();
}

class _SplashScreenState extends State<SplashScreen> {

final FirebaseAuth auth = FirebaseAuth.instance;
final User? user = FirebaseAuth.instance.currentUser;

@override
void initState() {
super.initState();
_initUser().whenComplete((){
setState(() {});
});
}

_initUser() async {
if (auth.currentUser != null) {
Timer(
Duration(seconds: 2),
() => Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) =>
MainScreen(user!)),
(Route<dynamic> route) => false),
);
} else {
Timer(Duration(seconds: 1),
() => Navigator.of(context).pushAndRemoveUntil(
MaterialPageRoute(
builder: (context) =>
MyHomePage()),
(Route<dynamic> route) => false),
);
}
}

@override
Widget build(BuildContext context) {

return Scaffold(
body: Center(
child: Text("Splash Screen"),
),

);
}
}

这是您未登录时传递的 MyHomePage 小部件。

class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: TextButton(
onPressed: (){

FirebaseService().signup(context);
},
child: Text('Google'),
),
)
);
}
}
class FirebaseService{

final FirebaseAuth auth = FirebaseAuth.instance;
final GoogleSignIn googleSignIn = GoogleSignIn();

Future<void> signup(BuildContext context) async {


final GoogleSignInAccount? googleSignInAccount = await googleSignIn.signIn();
if (googleSignInAccount != null) {
final GoogleSignInAuthentication googleSignInAuthentication =
await googleSignInAccount.authentication;
final AuthCredential authCredential = GoogleAuthProvider.credential(
idToken: googleSignInAuthentication.idToken,
accessToken: googleSignInAuthentication.accessToken);

// Getting users credential
UserCredential result = await auth.signInWithCredential(authCredential);
User? user = result.user;


if (user != null) {
Navigator.push(
context, MaterialPageRoute(builder: (context) => MainScreen(user)));
} // if result not null we simply call the MaterialpageRoute,
// for go to the HomePage screen
}
}

Future<void> signOutFromGoogle() async{
await googleSignIn.signOut();
await auth.signOut();
}
}

这是错误的内容

The following LateError was thrown building MainScreen(dirty, dependencies: [_InheritedProviderScope<Pro?>], state: _MainScreenState#d95a0):
LateInitializationError: Field '_instance@640075166' has not been initialized.

The relevant error-causing widget was:
MainScreen MainScreen:file:///F:/flutter%20project/good_man/lib/main.dart:75:25
When the exception was thrown, this was the stack:

抱歉,我忘记了主屏幕代码。下面是主屏代码

class MainScreen extends StatefulWidget {
const MainScreen(this.user);

final User user;

@override

_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
@override
Widget build(BuildContext context) {

final User user = widget.user;
final pro = Provider.of<Pro>(context);
return DefaultTabController(
length: 3,
child: Scaffold(

appBar: PreferredSize(
preferredSize: Size.fromHeight(60.0), // here the desired height
child: AppBar(
iconTheme: IconThemeData(color: Colors.black),
backgroundColor: pro.backColor_main,
elevation: 0.0,
centerTitle: true,
title: Text('aaa',
style: TextStyle(
fontFamily: 'Gugi',
fontSize: 20.sp,
color: Colors.black,
),),
),
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
DrawerHeader(
child: Text('Drawer Header'),
decoration: BoxDecoration(
color: Colors.white
),
),
ListTile(
title: Text('aaa'),
onTap: () {
Navigator.pop(context);
},
),

],
),
),
bottomNavigationBar: BottomBar(),
body: Stack(children: [

TabBarView(
children: [
MainTest(),
Text(''),
Main_User(user),
],
),
])),
);
}
}

很抱歉发布所有代码。没有使用 Late 的部分,但错误代码显示为 Late,我尝试删除缓存并启动

最佳答案

实际上错误是在当前用户的初始化中......您必须了解有状态小部件的生命周期......initState 没有保持流量。它只是初始化一些实例值...并且您尝试延迟 2 秒...所以这就是问题所在。

使用 FutureBuilder 进行_initUser(),当它获取所有数据时,然后继续下一个屏幕。

关于android - 抖动错误 LateInitializationError : Field '@' has not been initialized,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70832643/

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