【发布时间】:2021-09-21 02:39:32
【问题描述】:
我正在尝试使用(profileId: currentUser.id):
body: PageView(
children: <Widget>[
//Feed(),
ElevatedButton(
child: Text('Cerrar sesión'),
onPressed: logout,
),
SubirPost(currentUser: currentUser),
EditarPerfil(profileId: currentUser.id),
],
controller: pageController,
onPageChanged: onPageChanged,
physics: NeverScrollableScrollPhysics(),
),
但它给了我这个错误:
The property 'id' can't be unconditionally accessed because the receiver can be 'null'.
Try making the access conditional (using '?.') or adding a null check to the target ('!').
我已经尝试过 null 检查(例如:profileId: currentUser!.id),但是当我调试它时会抛出 Null check operator used on a null value 错误
id 在用户模型文件中声明如下:
class User {
final String id;
final String username;
final String email;
final String photoUrl;
final String displayName;
final String bio;
User(
{required this.id,
required this.username,
required this.email,
required this.photoUrl,
required this.displayName,
required this.bio});
factory User.fromDocument(DocumentSnapshot doc) {
return User(
id: doc['id'],
email: doc['email'],
username: doc['username'],
photoUrl: doc['photoUrl'],
displayName: doc['displayName'],
bio: doc['bio'],
);}}
对于 profileId,这是在另一个文件上:
final String profileId;
EditarPerfil({required this.profileId});
我已经尝试了所有我发现的东西
另外,当我尝试在用户模型 final String id 上使用 ? 时,我收到错误 The argument type 'String?' can't be assigned to the parameter type 'String'
与final String profileId 相同
我正在导入 cloud_firestore、firebase_storage 和 google_sign_in 包。
【问题讨论】:
-
您的问题是
currentUser变量为空,而不是id为空。当您的应用首次加载时,它还没有从 firestore 获得快照,因此您的用户为空。我认为您的两个最佳选择是处理用户为空并显示加载指示器(或类似)的可能性,或者将currentUser初始化为具有空值的User(即id= "") .最适合您的解决方案取决于您应用的架构。
标签: flutter dart google-cloud-firestore dart-null-safety