[英]Flutter non-nullable
I have a error The non-nullable local variable 'firebaseUser' must be assigned before it can be used.我有一个错误必须先分配不可为空的局部变量“firebaseUser”,然后才能使用它。 Try giving it an initializer expression
尝试给它一个初始化表达式
My code我的代码
if (firebaseUser != null) {
saveUserInfoToFirestore(firebaseUser).then((value) {
Navigator.pop(context);
Route route = MaterialPageRoute(builder: (c) => StoreHome());
Navigator.pushReplacement(context, route);
});
}
Checking a non-nullable variable to be null, is like say to a man with no legs he should run.检查一个不可为空的变量是否为空,就像对一个没有腿的人说他应该跑。
I guess you have something like this:我猜你有这样的事情:
User firebaseUser = FirebaseAuth.instance.currentUser;
So if you want to check about null, you have to make it nullable first.因此,如果要检查 null,则必须先使其可为 null。 Doing so with "?"
用“?”这样做after the typedeclaration.
在类型声明之后。
User? firebaseUser = FirebaseAuth.instance.currentUser;
For more info: https://dart.dev/null-safety欲了解更多信息: https ://dart.dev/null-safety
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.