简体   繁体   English

Flutter:GoogleSignInAccount 属性“身份验证”无法无条件访问,因为接收者可以为“空”

[英]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.

相关问题 无法无条件访问属性“文档”,因为接收者可以为“空”-Flutter - The property 'documents' cannot be unconditionally accessed because receiver can be 'null' -Flutter 无法无条件访问属性“isEmpty”,因为接收者可以为“null” - The property 'isEmpty" can't be unconditionally accessed because the receiver can be 'null' 无法无条件访问属性,因为接收者可以为空 - Property can't be unconditionally accessed because the receiver can be null 无法无条件访问该属性,因为接收者可以为“null”...? - The property can't be unconditionally accessed because the receiver can be 'null'...? 无法无条件访问属性“uid”,因为接收者可以为“null” - The property 'uid' can't be unconditionally accessed because the receiver can be 'null' 无法无条件访问该属性,因为接收者可以为“null” - The property can't be unconditionally accessed because the receiver can be 'null' 无法无条件访问属性“文件”,因为接收者可以为“空” - The property 'files' can't be unconditionally accessed because the receiver can be 'null' 无法无条件访问属性“窗口”,因为接收者可以为“空” - The property 'window' can't be unconditionally accessed because the receiver can be 'null' 无法无条件访问属性“长度”,因为接收者可以为“空” - The property 'length' can't be unconditionally accessed because the receiver can be 'null' 错误:无法无条件访问属性“sembol”,因为接收器可以为“null”flutter - error: The property 'sembol' can't be unconditionally accessed because the receiver can be 'null' flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM