我在Flutter中有一个匿名帐户,正在尝试使用Google的AuthCredential对其进行升级。我已验证凭据是使用有效的accessToken和idToken创建的。
因此,如果auth.currentUser()
是我的匿名FirebaseUser,则在下面调用linkAccountToGoogle()
会导致错误。
/// Upgrade an anonymous account by linking it to a Google account.
Future<FirebaseUser> linkAccountToGoogle() async {
final credential = await _getGoogleAuthCredential();
if (credential != null) {
try {
return auth.linkWithCredential(credential); // <=== THROWS ERROR
} catch (e) {
print(e);
}
}
return null;
}
/// Tries to sign-in silently first. May return `null`.
Future<AuthCredential> _getGoogleAuthCredential() async {
GoogleSignInAccount account;
try {
account = await _googleAuthService.signInSilently() ??
await _googleAuthService.signIn();
} catch (e) {
print(e);
}
final googleAuth = await account?.authentication;
if (account == null) {
print('Unable to retrieve Google account.');
} else if (googleAuth == null) {
print('Unable to authenticate to Google account (${account.email}).');
} else {
// print(
// 'accessToken: ${googleAuth.accessToken}, idToken: ${googleAuth.idToken}');
return GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken, idToken: googleAuth.idToken);
}
return null;
}
Stacktrace
I/flutter ( 3038): Credential: Instance of 'AuthCredential'
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): java.lang.IllegalArgumentException: Given String is empty or null
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at com.google.android.gms.common.internal.Preconditions.checkNotEmpty(Unknown Source:5)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at com.google.firebase.auth.EmailAuthProvider.getCredential(Unknown Source:2)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at io.flutter.plugins.firebaseauth.FirebaseAuthPlugin.handleLinkWithEmailAndPassword(FirebaseAuthPlugin.java:272)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at io.flutter.plugins.firebaseauth.FirebaseAuthPlugin.onMethodCall(FirebaseAuthPlugin.java:122)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:200)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at io.flutter.view.FlutterNativeView$PlatformMessageHandlerImpl.handlePlatformMessage(FlutterNativeView.java:188)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:202)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at android.os.MessageQueue.next(MessageQueue.java:325)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at android.os.Looper.loop(Looper.java:142)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at android.app.ActivityThread.main(ActivityThread.java:6694)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
E/MethodChannel#plugins.flutter.io/firebase_auth( 3038): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)
E/flutter ( 3038): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, Given String is empty or null, null)
E/flutter ( 3038): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
E/flutter ( 3038): #1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:302:33)
E/flutter ( 3038): <asynchronous suspension>
E/flutter ( 3038): #2 FirebaseAuth.linkWithCredential (file:///home/jacob/.pub-cache/hosted/pub.dartlang.org/firebase_auth-0.8.0+1/lib/src/firebase_auth.dart:371:54)
我尝试过的事情
我要链接的Google帐户已经是该项目的用户,因此我从Firebase控制台中删除了该google用户。然后使用flutter clean
flutter run
再试一次。