英文:
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.
这是我的代码
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.
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论