简体   繁体   English

无法将“OAuthCredential”类型的值分配给“GoogleAuthCredential”类型的变量

[英]A value of type 'OAuthCredential' can't be assigned to a variable of type 'GoogleAuthCredential'

This is my code for google sign-in..这是我的谷歌登录代码..

             onPressed: () async{
                final GoogleSignInAccount newuser= await GoogleSignIn().signIn();
                final  GoogleSignInAuthentication newuserauth= await 
         googleUser.authentication;
                final GoogleAuthCredential cred= GoogleAuthProvider.credential(accessToken: 
                 newuserauth.accessToken,idToken: newuserauth.idToken);
                await FirebaseAuth.instance.signInWithCredential(cred);
              },

And the errors i am getting are as follows..我得到的错误如下..

        error: A value of type 'OAuthCredential' can't be assigned to a variable of type 
        'GoogleAuthCredential'. (invalid_assignment at [firebase] lib\firstpage.dart:147)

        error: Undefined name 'googleUser'. (undefined_identifier at [firebase] 
            lib\firstpage.dart:145)
         error: A value of type 'GoogleSignInAccount?' can't be assigned to a variable of type 
        'GoogleSignInAccount'. (invalid_assignment at [firebase] lib\firstpage.dart:144)

These are the dependencies in my pubspec.yaml..这些是我的 pubspec.yaml 中的依赖项。

      dependencies:
        flutter:
          sdk: flutter
           firebase_auth: ^3.2.0
           firebase_core : ^1.10.0
           flutter_spinkit: ^5.1.0
           cloud_firestore: ^3.1.0
           google_sign_in: ^5.2.1

There is multiple problems in you code as follows:您的代码中存在多个问题,如下所示:

Line 2: GoogleSignIn().signIn() returns GoogleSignInAccount?第 2 行: GoogleSignIn().signIn()返回GoogleSignInAccount? which mean it could be null but you're using GoogleSignInAccount which mean it can't be null,,, so change it to (This is the last error you got):这意味着它可能是 null 但您使用的是GoogleSignInAccount这意味着它不能是 null,所以将其更改为(这是您遇到的最后一个错误):

   final GoogleSignInAccount? newuser= await GoogleSignIn().signIn();

Line 3 & 4: You used the variable name newuser not googleUser change one of them (the second error)第 3 行和第 4 行:您使用变量名newuser而不是googleUser更改其中之一(第二个错误)

Line 5 & 6: GoogleAuthProvider.credential(..) returns OAuthCredential not GoogleAuthCredential and that is the first error you got.第 5 行和第 6 行: GoogleAuthProvider.credential(..)返回OAuthCredential而不是GoogleAuthCredential ,这是您遇到的第一个错误。

However, with final you don't need to specify the variable type which is one of the Dart advantages.但是,使用final您不需要指定变量类型,这是 Dart 的优势之一。

Moreover, you'll get error on newuser.authentication because newuser, as mentioned before, could be null so you can't access authentication ... the way I love to do it, because I don't like to deal with null values is to return from the function before using it if its null.此外,您会在newuser.authentication上遇到错误,因为如前所述,newuser 可能是 null 所以您无法访问authentication ......我喜欢这样做,因为我不喜欢处理 null 值如果它的 null 在使用它之前从 function 返回。

So the whole code would be (I added the types so you could see the difference, but you don't need them ):所以整个代码将是(我添加了类型,以便您可以看到差异,但您不需要它们):

final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
if (googleUser == null) return null;
  
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final OAuthCredential credential = GoogleAuthProvider.credential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
  );
await FirebaseAuth.instance.signInWithCredential(credential);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “对象?”类型的值不能分配给“Tasker”类型的变量? - A value of type 'Object?' can't be assigned to a variable of type 'Tasker?' 错误:无法将“AuthResult”类型的值分配给“FirebaseUser”类型的变量 - Error: A value of type 'AuthResult' can't be assigned to a variable of type 'FirebaseUser' 无法将“AuthResult”类型的值分配给“FirebaseUser”类型的变量 - A value of type 'AuthResult' can't be assigned to a variable of type 'FirebaseUser' 'Future 类型的值<string> '不能分配给'String'类型的变量</string> - A value of type 'Future<String>' can't be assigned to a variable of type 'String' 'Future 类型的值<int> '不能分配给'int'类型的变量</int> - A value of type 'Future<int>' can't be assigned to a variable of type 'int' &#39;String?&#39; 类型的值不能分配给“字符串”类型的变量。 预期有 1 个位置参数,但找到 0 个 - A value of type 'String?' can't be assigned to a variable of type 'String'. and 1 positional argument(s) expected, but 0 found 'Future 类型的值<firebaseuser> Function()' 不能分配给“用户”类型的变量</firebaseuser> - A value of type 'Future<FirebaseUser> Function()' can't be assigned to a variable of type 'User' getDownloadURL - 类型为“Future”的值<dynamic> &#39; 不能分配给类型为 &#39;String&#39; 的变量 - getDownloadURL - A value of type 'Future<dynamic>' can't be assigned to a variable of type 'String' 'Future 类型的值<querysnapshot<map<string, dynamic> &gt;&gt;' 不能分配给“QuerySnapshot”类型的变量<object?> ' </object?></querysnapshot<map<string,> - A value of type 'Future<QuerySnapshot<Map<String, dynamic>>>' can't be assigned to a variable of type 'QuerySnapshot<Object?>' Future builder flutter firebase 错误:“Iterable”类型的值<BlogPost> &#39; 不能分配给类型为 &#39;List 的变量<BlogPost> &#39; - Future builder flutter firebase error: A value of type 'Iterable<BlogPost>' can't be assigned to a variable of type 'List<BlogPost>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM