這是我的谷歌登錄代碼..
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);
},
我得到的錯誤如下..
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)
這些是我的 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
uj5u.com熱心網友回復:
您的代碼存在多個問題,如下所示:
第 2 行:GoogleSignIn().signIn()回傳GoogleSignInAccount?這意味著它可能為空,但您正在使用GoogleSignInAccount這意味著它不能為空,因此將其更改為(這是您遇到的最后一個錯誤):
final GoogleSignInAccount? newuser= await GoogleSignIn().signIn();
第 3 行和第 4 行:您使用的變數名稱newuser未googleUser更改其中之一(第二個錯誤)
第 5 行和第 6 行:不GoogleAuthProvider.credential(..)回傳,這是您遇到的第一個錯誤。OAuthCredentialGoogleAuthCredential
但是,final您無需指定變數型別,這是 Dart 的優勢之一。
此外,您會收到錯誤訊息,newuser.authentication因為如前所述,newuser 可能為空,因此您無法訪問authentication……我喜歡這樣做的方式,因為我不喜歡處理空值是從使用它之前的函式,如果它為空。
所以整個代碼將是(我添加了型別以便您可以看到差異,但您不需要它們):
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/365989.html
標籤:火力基地 扑 Firebase 身份验证 谷歌登录帐户
