Firebase: 在Flutter中对空值使用了空检查运算符

huangapple go评论64阅读模式
英文:

Firebase: Null check operator used on a null value on Flutter

问题

I have configured my Firebase credentials. But yet I am still getting errors. Here is what the simulator is showing me.

Firebase: 在Flutter中对空值使用了空检查运算符

这是我的代码

await Auth().createUserWithEmailAndPassword(
    email: emailRegistered, password: passwordRegistered);
    
Navigator.push(
    context,
    MaterialPageRoute(
    builder: (context) => const MainNav()),
);

然后导航到下一个屏幕,有这一行,据称是引发错误的地方

final user = FirebaseAuth.instance.currentUser!;

我会欣然接受任何与此相关的答案。

英文:

I have configured my Firebase credentials. But yet I am still getting errors. Here is what the simulator is showing me. Firebase: 在Flutter中对空值使用了空检查运算符

Here is my code

await Auth().createUserWithEmailAndPassword(
        email: emailRegistered, password: passwordRegistered);

    Navigator.push(
      context,
      MaterialPageRoute(
      builder: (context) => const MainNav()),
    );

Which then navigates to the next screen and has this line which is allegedly calling the error

final user = FirebaseAuth.instance.currentUser!;

I would appreciate any sort of answers in regards to this.

答案1

得分: 1

你可以尝试使用空安全运算符 ?. 而不是空检查运算符 !

final user = FirebaseAuth.instance.currentUser?.uid;

还可以在访问之前检查用户是否已经认证:

if (FirebaseAuth.instance.currentUser != null) {
  final user = FirebaseAuth.instance.currentUser!.uid;
  // 处理用户情况
} else {
  // 处理用户未认证的情况
}
英文:

You can try by using null-aware operator ?. instead of the null check operator !.

final user = FirebaseAuth.instance.currentUser?.uid;

Also check if the user is authenticated before accessing by:

if (FirebaseAuth.instance.currentUser != null) {
  final user = FirebaseAuth.instance.currentUser!.uid;
  // do something with user
} else {
  // handle the case when the user is not authenticated
}

huangapple
  • 本文由 发表于 2023年5月15日 09:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定