【问题标题】:The property 'id' can't be unconditionally accesed because the receiver can be 'null'无法无条件访问属性“id”,因为接收者可以为“null”
【发布时间】: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_firestorefirebase_storagegoogle_sign_in 包。

【问题讨论】:

  • 您的问题是 currentUser 变量为空,而不是 id 为空。当您的应用首次加载时,它还没有从 firestore 获得快照,因此您的用户为空。我认为您的两个最佳选择是处理用户为空并显示加载指示器(或类似)的可能性,或者将currentUser 初始化为具有空值的User(即id = "") .最适合您的解决方案取决于您应用的架构。

标签: flutter dart google-cloud-firestore dart-null-safety


【解决方案1】:

当您编写 currentUser!.id 时,您要确保 currentUser 不会为空。因此,当您的 currentUser 为 null 时,您将收到此错误:Null check operator used on a null value。因此,如果在调用 profileId 时有可能获得空值,则必须使配置文件 id 为空。为此,只需在String? idUser 类中将id 声明为可为空。或者我推荐的另一种方法是使用?? 运算符(reference),这只是一个代表if null 的条件。您可以在 UserId 的赋值语句中使用它。 像这样:

body: PageView(
    children: <Widget>[
      //Feed(),
      ElevatedButton(
        child: Text('Cerrar sesión'),
        onPressed: logout,
      ),
      SubirPost(currentUser: currentUser),
      EditarPerfil(profileId: currentUser.id ?? ''),   // this is a check if the id is not null then it will return the actual value otherwise it will return the empty string.
    ],
    controller: pageController,
    onPageChanged: onPageChanged,
    physics: NeverScrollableScrollPhysics(),
  ),

【讨论】:

  • 当我包含 currentUser.id 时? ' ' 它给了我错误“左操作数不能为空,所以右操作数永远不会执行(死空感知)”所以我将 'String id' 声明为 'String? id' 在 User 类中,它解决了死空感知,但保留了主要问题中提到的错误。
  • 如果您在访问 userId 时得到空值,则此 ?? 运算符将用空字符串 '' 替换它。现在请尝试在此之后更新问题和错误消息。
猜你喜欢
  • 2021-07-13
  • 2022-08-15
  • 2021-08-05
  • 2022-08-15
  • 2022-01-11
  • 2021-09-21
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
相关资源
最近更新 更多