0

我是 StackOverflow 的新手。但是我从 StackOverflow 看到了很多答案,但我找不到与我的问题类似的答案。当我从 firestore 中的特定文档中检索字段值时,await 函数会显示一个异常,即_CastError (Null check operator used on a null value)这是显示此异常

这是我的 Firebase 收藏图片。我需要这样,当用户使用他们的电子邮件和密码登录时,将从 firebase field获取身份值。我在 Firebase 中有用户数据,但是当我运行代码并尝试登录时,它会显示在此异常中。 但我无法解决这个问题。

这是我的代码:

try {
  final newUser = await _auth.signInWithEmailAndPassword(email: _email, password: _pass);
  if (newUser != null) {
    final user = await _auth.currentUser!;
    final userID = user.uid;
    print('user id: ');
    print(userID);
                                  
    //This is my retrieve code
    await FirebaseFirestore.instance
                           .collection('Students')
                           .doc(userID)
                           .get()
                           .then((value) {
                             return value.data()!['identity'];
                           });

    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => loginVerification(),
      ),
    );
  }
}

在这里, “学生”是我的 Firebase 集合名称, 用户 ID是我的特定文档名称(当用户使用他们的电子邮件和密码登录时,该用户的 UID 将在此处设置),“身份”是字段名称。

现在我想要这样,当用户使用他们的电子邮件和密码登录时,然后从 firebase 获取身份值。如果有人有这个答案,那么告诉我如何解决这个问题。

4

0 回答 0