LateInitializationError: Field 'user' has not been initialized
LateInitializationError: Field 'user' has not been initialized
我对用户初始值有疑问。
我有这种类型的源代码:“https:// paste.ofcode.org/ycHT4YFsDGSQXa68JiKJRH”
链接中的重要行:
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 240,
child: FutureBuilder(
future: getCurrentUser(),
builder: (context, snapshot) {
return ListView.separated(
itemBuilder: (context, index) {
print(index);
return GetUserName(documentId: user?.uid);
},
separatorBuilder: (context, index) {
return SizedBox(width: 10);
},
itemCount: myCards.length,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
);
}),
我也有这个class,
class GetUserName extends StatelessWidget {
final String documentId;
final int index = 0;
GetUserName({
required this.documentId,
});
@override
Widget build(BuildContext context) {
// get the collection
CollectionReference users = FirebaseFirestore.instance.collection('Users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(documentId).get(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
return MyCard(
userName: data['username'],
card: myCards[index],
);
// Text('First Name: ${data['username']}');
} else {
print("here");
return MyCard(
userName: "Waiting",
card: myCards[index],
);
}
return Text('loading..');
},
);
}
}
我在 90th
行中使用 FutureBuilder,它的未来值是 getCurrentUser()
这个函数创建这个值 user = await _auth.currentUser;
然后我在第 96 行的 GetUserName 函数中给了 user?.uid 参数(目的:将当前用户 ID 给 GetUserName)。
我在 MyCard 中成功看到当前用户名,但我也有错误
======== Exception caught by widgets library =======================================================
The following LateError was thrown building:
LateInitializationError: Field 'user' has not been initialized.
我该如何解决这个问题?
使用 User? user
而不是 late final user
以便能够在加载用户之前检查空值。在获取用户时的 getCurrentUser()
方法中,使用 setState 更新用户和当前状态:
Future getCurrentUser() async {
final loadedUser = await _auth.currentUser;
if (loadedUser != null) {
setState(() {
user = loadedUser;
});
}
}
我对用户初始值有疑问。
我有这种类型的源代码:“https:// paste.ofcode.org/ycHT4YFsDGSQXa68JiKJRH”
链接中的重要行:
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
height: 240,
child: FutureBuilder(
future: getCurrentUser(),
builder: (context, snapshot) {
return ListView.separated(
itemBuilder: (context, index) {
print(index);
return GetUserName(documentId: user?.uid);
},
separatorBuilder: (context, index) {
return SizedBox(width: 10);
},
itemCount: myCards.length,
shrinkWrap: true,
scrollDirection: Axis.horizontal,
);
}),
我也有这个class,
class GetUserName extends StatelessWidget {
final String documentId;
final int index = 0;
GetUserName({
required this.documentId,
});
@override
Widget build(BuildContext context) {
// get the collection
CollectionReference users = FirebaseFirestore.instance.collection('Users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(documentId).get(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data =
snapshot.data!.data() as Map<String, dynamic>;
return MyCard(
userName: data['username'],
card: myCards[index],
);
// Text('First Name: ${data['username']}');
} else {
print("here");
return MyCard(
userName: "Waiting",
card: myCards[index],
);
}
return Text('loading..');
},
);
}
}
我在 90th
行中使用 FutureBuilder,它的未来值是 getCurrentUser()
这个函数创建这个值 user = await _auth.currentUser;
然后我在第 96 行的 GetUserName 函数中给了 user?.uid 参数(目的:将当前用户 ID 给 GetUserName)。
我在 MyCard 中成功看到当前用户名,但我也有错误
======== Exception caught by widgets library =======================================================
The following LateError was thrown building:
LateInitializationError: Field 'user' has not been initialized.
我该如何解决这个问题?
使用 User? user
而不是 late final user
以便能够在加载用户之前检查空值。在获取用户时的 getCurrentUser()
方法中,使用 setState 更新用户和当前状态:
Future getCurrentUser() async {
final loadedUser = await _auth.currentUser;
if (loadedUser != null) {
setState(() {
user = loadedUser;
});
}
}