Flutter – 未处理的异常: 在空值上使用了空值检查运算符

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

Flutter - Unhandled Exception: Null check operator used on a null value

问题

抱歉,你要求只返回翻译好的部分,以下是代码部分的翻译:

我正在尝试使用GetX控制器从FirebaseFirestore文档初始化字段,但我遇到以下错误:

> 未处理的异常:在空值上使用了空检查运算符。

控制器已创建,但由于在空值上使用了空检查运算符,因此未初始化。以下是我的代码:

class DataController extends GetxController{
  DocumentSnapshot? userDocument;
  FirebaseAuth auth = FirebaseAuth.instance;

  getUserProfileData() {
    FirebaseFirestore.instance
        .collection('users')
        .doc(auth.currentUser!.uid) //在空值上使用了空检查运算符
        .snapshots()
        .listen((event) {
      userDocument = event;
    });
  }

  @override
  void onInit() {
    super.onInit();
    getUserProfileData();
  }
}

我的错误日志:

E/flutter (11591): #1      DataController.onInit
data_controller.dart:76
E/flutter (11591): #2      GetLifeCycleBase._onStart
lifecycle.dart:66
E/flutter (11591): #3      InternalFinalCallback.call
lifecycle.dart:12
E/flutter (11591): #4      GetInstance._startController
get_instance.dart:253
E/flutter (11591): #5      GetInstance._initDependencies
get_instance.dart:204
E/flutter (11591): #6      GetInstance.find
get_instance.dart:301
E/flutter (11591): #7      GetInstance.put
get_instance.dart:86
E/flutter (11591): #8      Inst.put
extension_instance.dart:89
E/flutter (11591): #9      main
main.dart:23
E/flutter (11591): <asynchronous suspension>
E/flutter (11591):

如果你需要更多的帮助,请随时告诉我。

英文:

I am trying to initialize fields from a FirebaseFirestore document using GetXcontroller but I am getting the following error:

> Unhandled Exception: Null check operator used on a null value.

The Controller gets created but it doesn't get initialized because of
the null check operator used on null value. Here's my code:

class DataController extends GetxController{
  DocumentSnapshot? userDocument;
  FirebaseAuth auth = FirebaseAuth.instance;

  getUserProfileData() {
    FirebaseFirestore.instance
        .collection(&#39;users&#39;)
        .doc(auth.currentUser!.uid) //Null check operator used on a null value
        .snapshots()
        .listen((event) {
      userDocument = event;
    });
  }

  @override
  void onInit() {
    super.onInit();
    getUserProfileData();
  }
}

My error log:

E/flutter (11591): #1      DataController.onInit
data_controller.dart:76
E/flutter (11591): #2      GetLifeCycleBase._onStart
lifecycle.dart:66
E/flutter (11591): #3      InternalFinalCallback.call
lifecycle.dart:12
E/flutter (11591): #4      GetInstance._startController
get_instance.dart:253
E/flutter (11591): #5      GetInstance._initDependencies
get_instance.dart:204
E/flutter (11591): #6      GetInstance.find
get_instance.dart:301
E/flutter (11591): #7      GetInstance.put
get_instance.dart:86
E/flutter (11591): #8      Inst.put
extension_instance.dart:89
E/flutter (11591): #9      main
main.dart:23
E/flutter (11591): &lt;asynchronous suspension&gt;
E/flutter (11591):

答案1

得分: 1

错误提示您,在此表达式中:
auth.currentUser!.uid
currentUser 的值为 null。这意味着用户在此时尚未通过身份验证。请确保在执行代码的这一点之前进行用户登录。作为防护措施,您还应手动检查用户在调用此链之前是否已经通过身份验证。例如,可以这样做:

    FirebaseFirestore.instance
        .collection('users')
        .doc(auth.currentUser!.uid) //现在可以在这里使用强制解包
        // ...
}```

<details>
<summary>英文:</summary>

The error tells you that in this expression

auth.currentUser!.uid

the value of `currentUser` is null. This means that the user is not authenticated at that point. Make sure to perform user login before reaching this point of your code. As a safeguard you should also manually check if the user is authenticated before calling this chain. For example via

if (auth.currentUser != null) {
FirebaseFirestore.instance
.collection('users')
.doc(auth.currentUser!.uid) //Force unwrapping is now fine here
// ...
}


</details>



huangapple
  • 本文由 发表于 2023年2月18日 18:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492592.html
匿名

发表评论

匿名网友

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

确定