错误:Runner[463:34314] 颤动:LateInitializationError:字段“名称”尚未初始化。
我最近更新为使用 firebase core 和 nullsafety,并对我不太了解的代码进行了一些更改,我是这里编程的新手。无论如何,我已经尝试阅读有关它的类似主题 here,但我仍然不太了解它。我知道我很可能没有在 initstate 中正确使用 name,但这就是我所理解的。有人可以提供解决以下错误所需的示例代码吗?
2021-04-10 17:59:41.331476-0700 Runner[463:34314] 颤动:LateInitializationError:字段“名称”尚未初始化。
class MyService extends StatefulWidget {
@override
_MyServiceState createState() => _MyServiceState();
}
class _MyServiceState extends State<MyService> {
late String name, email;
Widget currentWidget = BackgroundBetcher();
@override
void initState() {
// TODO: implement initState
super.initState();
findNameAnEmail();
}
Future<Null> findNameAnEmail() async {
await Firebase.initializeApp().then((value) async {
FirebaseAuth.instance.authStateChanges().listen((event) {
setState(() {
name = event!.displayName!;
email = event.email!;
});
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: MyStyle().primaryColor,
),
drawer: buildDrawer(),
body: currentWidget,
);
}
Drawer buildDrawer() {
return Drawer(
child: Stack(
children: [
Column(
children: [
buildUserAccountsDrawerHeader(),
buildListTileShowCartoonList(),
buildListTileInformation(),
],
),
buildSignOut(),
],
),
);
}
ListTile buildListTileShowCartoonList() {
return ListTile(
leading: Icon(
Icons.face,
size: 36,
),
title: Text('Manual Location Update'),
subtitle: Text('Send a single location update'),
onTap: () {
setState(() {
currentWidget = PageWidget();
});
Navigator.pop(context);
},
);
}
ListTile buildListTileInformation() {
return ListTile(
leading: Icon(
Icons.perm_device_info,
size: 36,
),
title: Text('Background Location Fetch Log'),
subtitle: Text('History of recorded locations'),
onTap: () {
setState(() {
currentWidget = BackgroundBetcher();
});
Navigator.pop(context);
},
);
}
UserAccountsDrawerHeader buildUserAccountsDrawerHeader() {
return UserAccountsDrawerHeader(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/wall.jpg'), fit: BoxFit.cover),
),
accountName: MyStyle().titleH3(name),
accountEmail: MyStyle().titleH3(email),
currentAccountPicture: Image.asset('images/logo.png'),
);
}
Column buildSignOut() {
return Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ListTile(
onTap: () async {
await Firebase.initializeApp().then((value) async {
await FirebaseAuth.instance.signOut().then((value) =>
Navigator.pushNamedAndRemoveUntil(
context, '/authen', (route) => false));
});
},
tileColor: MyStyle().darkColor,
leading: Icon(
Icons.exit_to_app,
color: Colors.white,
size: 36,
),
title: MyStyle().titleH2White('Sign Out'),
subtitle: MyStyle().titleH3White('Sign Out & Go to Authen'),
),
],
);
}
}
答案 0 :(得分:1)
findNameAnEmail
是一种异步方法,因此,虽然在 initState
中调用它,但 Flutter 不会在调用 build
之前等待它完成。这会导致您的延迟字段在设置之前被访问。
为了在构建之前等待 Future
完成,请考虑使用 FutureBuilder
。
答案 1 :(得分:0)
我对 Flutter 很陌生,但这对我有用。
@override
void initState() {
// TODO: implement initState
super.initState();
findNameAndEmail().whenComplete(() {
setState(() {});
});
}