[英]Flutter: GoogleSignInAccount The property 'authentication' can't be unconditionally accessed because the receiver can be 'null'
I'm new to flutter.我是 flutter 的新手。 Am practicing google account log in. When I define GoogleSignInAccount, there is an error: "A value of type 'GoogleSignInAccount?'
正在练习 google 帐户登录。当我定义 GoogleSignInAccount 时,出现错误:“A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount'."
不能分配给“GoogleSignInAccount”类型的变量。”
final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
Other people saying that we can use '?'其他人说我们可以使用“?” to solve this problem.
来解决这个问题。 It works.
有用。 However, another issue pop up: "The property 'authentication' can't be unconditionally accessed because the receiver can be 'null'."
但是,弹出另一个问题:“属性'authentication'不能无条件访问,因为receiver可以是'null'。”
final GoogleSignInAccount? googleUser = await _googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
Any advice would be great.任何建议都会很棒。 ty
泰
You get that warning because googleUser has a nullable type ( GoogleSignInAccount?
) in order to use it in googleAuth you can mark it with a bang (null-aware) operator like this googleUser.;authentication;
您会收到该警告,因为googleUser具有可为空的类型(
GoogleSignInAccount?
),以便在 googleAuth 中使用它,您可以使用像googleUser.;authentication;
这样的bang(null-aware)运算符对其进行标记。 . . However, only use that approach if you are 100% certain this will never be null, in which case you could simply use a non-nullable type (without the "?") if possible.
但是,仅当您 100% 确定这永远不会是 null 时才使用该方法,在这种情况下,如果可能,您可以简单地使用不可为空的类型(没有“?”)。
Alternatively, you can use control flow statements
to only initialize googleAuth once googleUser is initialized with a non-null value.或者,您可以使用
control flow statements
仅在使用非空值初始化 googleUser 后初始化 googleAuth。
For example a null-check like this:例如这样的空检查:
if(googleUser !=null) {
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
}
More context:更多背景:
The reason why you get the error: "A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount'."
您收到错误的原因:
"A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount'."
"A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 'GoogleSignInAccount'."
is that _googleSignIn.signIn();
是
_googleSignIn.signIn();
has a nullable return type of 'GoogleSignInAccount?'
有一个可以为空的返回类型
'GoogleSignInAccount?'
(because it can fail and don´t return a user), hence you cant assign it to a non-nullable variable with the type 'GoogleSignInAccount'.
(因为它可能会失败并且不会返回用户),因此您不能将其分配给类型为
'GoogleSignInAccount'.
的不可为空的变量。
FYI: It most likely makes more sense to add the null check around the _googleSignIn.signIn();仅供参考:在 _googleSignIn.signIn(); 周围添加 null 检查很可能更有意义;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.