You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter集成Google登录触发PlatformException(ApiException:10)求助

Flutter Google登录 ApiException:10 解决方案

ApiException:10是Google登录最常见的错误,主要由证书指纹配置、文件不匹配或代码异步处理问题导致,以下是针对性解决步骤:

1. 配置正确的SHA-1证书指纹

这是错误10的核心诱因:

  • 生成Debug环境SHA-1:
    终端执行命令(Windows用户替换路径为C:\Users\<你的用户名>\.android\debug.keystore):
    keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
    
  • 生成Release环境SHA-1(若需发布版本支持登录):
    替换为你的签名密钥路径与别名:
    keytool -list -v -keystore <你的签名密钥路径> -alias <密钥别名>
    
  • 登录Firebase控制台,进入项目设置→安卓应用,将生成的SHA-1添加到对应应用中,重新下载google-services.json文件并替换到android/app目录下。

2. 修复代码中的异步处理问题

按钮点击事件修复

原代码未等待登录方法完成,导致状态错误且异常未捕获,修改后:

OutlinedButton(
  onPressed: () async {
    setState(() {
      _isSigningIn = true;
    });
    try {
      await AuthController.signInWithGoogle(context: context);
    } catch (e) {
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text("登录失败:${e.toString()}")),
      );
    } finally {
      setState(() {
        _isSigningIn = false;
      });
    }
  },
),

登录控制器代码修复

原代码未捕获googleSignIn.signIn()的异常,导致未处理的运行时错误,修改后:

static Future<User> signInWithGoogle({@required BuildContext context}) async {
  FirebaseAuth auth = FirebaseAuth.instance;
  User user;
  log("first of all ");
  final GoogleSignIn googleSignIn = GoogleSignIn();
  
  try {
    final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
    log('++++++++++++++++++++++++++++ after await ');
    
    if (googleSignInAccount != null) {
      log('++++++++++++++++++++++++++++ nor null ');
      final GoogleSignInAuthentication googleSignInAuthentication =
          await googleSignInAccount.authentication;
      final AuthCredential credential = GoogleAuthProvider.credential(
        accessToken: googleSignInAuthentication.accessToken,
        idToken: googleSignInAuthentication.idToken,
      );
      
      try {
        final UserCredential userCredential =
            await auth.signInWithCredential(credential);
        user = userCredential.user;
      } on FirebaseAuthException catch (e) {
        if (e.code == 'account-exists-with-different-credential') {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text("该账号已关联其他登录方式")),
          );
        } else if (e.code == 'invalid-credential') {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(content: Text("无效的登录凭证")),
          );
        }
      } catch (e) {
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(content: Text("登录出错:${e.toString()}")),
        );
      }
    }
  } catch (e) {
    log("Google登录失败:${e.toString()}");
    rethrow;
  }
  
  return user;
}

3. 检查依赖与配置兼容性

  • 更新pubspec.yaml中的Firebase依赖到稳定兼容版本:
    dependencies:
      firebase_auth: ^4.10.0
      google_sign_in: ^5.4.0
    
    执行flutter pub upgrade完成更新。
  • 更新安卓build.gradle中的google-services插件版本:
    将classpath 'com.google.gms:google-services:4.3.13'改为classpath 'com.google.gms:google-services:4.4.0'(或最新稳定版)。
  • 确保android/app/src/main/AndroidManifest.xml添加了互联网权限:
    <uses-permission android:name="android.permission.INTERNET" />
    

4. 清理项目缓存并重构

执行以下命令清理旧配置残留,重新构建项目:

flutter clean
flutter pub get
cd android && ./gradlew clean
cd ..
flutter run

内容的提问来源于stack exchange,提问作者Esraa Gamal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 05:54:53