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

Flutter旧应用更新后Google SignIn功能失效求助

Flutter Google SignIn 功能异常排查求助

问题描述

更新旧Flutter应用时,Google SignIn功能无法正常使用,运行时提示 "An unknown error occurred"。

使用的依赖版本

firebase_ui_auth: ^1.5.0
firebase_auth: ^4.6.3
firebase_ui_oauth_google: ^1.2.4
firebase_dynamic_links: ^5.3.3
google_sign_in: ^6.1.4

认证核心代码

@override
Widget build(BuildContext context) {
  return StreamBuilder<User?>(
    stream: FirebaseAuth.instance.authStateChanges().distinct(),
    builder: (context, snapshot) {
      // 未认证时显示登录界面
      if (snapshot.hasData == false) {
        return SignInScreen(
          // providers: providers,
          headerBuilder: (context, constraints, _) {
            return SignInHeader();
          },
        );
      }
      _checkEmailVerification();
      // 已认证显示主界面
      return _buildMainScreen();
    },
  );
}

IDE日志输出

I/ViewRootImpl@eeb58b[MainActivity](14185): ViewPostIme pointer 0
I/ViewRootImpl@eeb58b[MainActivity](14185): ViewPostIme pointer 1
I/DecorView(14185): [INFO] isPopOver=false, config=true
I/DecorView(14185): updateCaptionType >> DecorView@2cb9d4c[], isFloating=false, isApplication=true, hasWindowControllerCallback=true, hasWindowDecorCaption=false
D/DecorView(14185): setCaptionType = 0, this = DecorView@2cb9d4c[]
I/DecorView(14185): getCurrentDensityDpi: from real metrics. densityDpi=420 msg=resources_loaded
I/ViewRootImpl@5631977[SignInHubActivity](14185): setView = com.android.internal.policy.DecorView@2cb9d4c TM=true
D/InputTransport(14185): Input channel destroyed: 'ClientS', fd=132
I/ViewRootImpl@5631977[SignInHubActivity](14185): Relayout returned: old=(0,0,1080,2280) new=(0,0,1080,2280) req=(1080,2280)0 dur=6 res=0x7 s={true 518689324656} ch=true fn=-1
D/OpenGLRenderer(14185): eglCreateWindowSurface
I/ViewRootImpl@5631977[SignInHubActivity](14185): [DP] dp(1) 1 android.view.ViewRootImpl.reportNextDraw:11442 android.view.ViewRootImpl.performTraversals:4198 android.view.ViewRootImpl.doTraversal:2924 
D/ViewRootImpl@5631977[SignInHubActivity](14185): Creating frameDrawingCallback nextDrawUseBlastSync=false reportNextDraw=true hasBlurUpdates=false
D/ViewRootImpl@5631977[SignInHubActivity](14185): Creating frameCompleteCallback
D/ViewRootImpl@5631977[SignInHubActivity](14185): Received frameDrawingCallback frameNum=1. Creating transactionCompleteCallback=false
D/ViewRootImpl@5631977[SignInHubActivity](14185): Received frameCompleteCallback  lastAcquiredFrameNum=1 lastAttemptedDrawFrameNum=1
I/ViewRootImpl@5631977[SignInHubActivity](14185): [DP] pdf(0) 1 android.view.ViewRootImpl.lambda$addFrameCompleteCallbackIfNeeded$3$ViewRootImpl:5000 android.view.ViewRootImpl$$ExternalSyntheticLambda16.run:6 android.os.Handler.handleCallback:938 
I/ViewRootImpl@5631977[SignInHubActivity](14185): [DP] rdf()
D/ViewRootImpl@5631977[SignInHubActivity](14185): reportDrawFinished (fn: -1) 
I/ViewRootImpl@eeb58b[MainActivity](14185): MSG_WINDOW_FOCUS_CHANGED 0 1
D/InputTransport(14185): Input channel destroyed: 'ClientS', fd=147
D/InsetsSourceConsumer(14185): ensureControlAlpha: for ITYPE_NAVIGATION_BAR on com.eis.lejog.lejog/com.google.android.gms.auth.api.signin.internal.SignInHubActivity
D/InsetsSourceConsumer(14185): ensureControlAlpha: for ITYPE_STATUS_BAR on com.eis.lejog.lejog/com.google.android.gms.auth.api.signin.internal.SignInHubActivity
I/FA      (14185): Application backgrounded at: timestamp_millis: 1689168021571
D/InsetsSourceConsumer(14185): ensureControlAlpha: for ITYPE_NAVIGATION_BAR on com.eis.lejog.lejog/com.google.android.gms.auth.api.signin.internal.SignInHubActivity
D/InsetsSourceConsumer(14185): ensureControlAlpha: for ITYPE_STATUS_BAR on com.eis.lejog.lejog/com.google.android.gms.auth.api.signin.internal.SignInHubActivity
I/ViewRootImpl@eeb58b[MainActivity](14185): stopped(false) old=false
I/DecorView(14185): notifyKeepScreenOnChanged: keepScreenOn=false
D/InsetsSourceConsumer(14185): ensureControlAlpha: for ITYPE_NAVIGATION_BAR on com.eis.lejog.lejog/com.eis.lejog.lejog.MainActivity
D/InsetsSourceConsumer(14185): ensureControlAlpha: for ITYPE_STATUS_BAR on com.eis.lejog.lejog/com.eis.lejog.lejog.MainActivity
I/ViewRootImpl@eeb58b[MainActivity](14185): Relayout returned: old=(0,0,1080,2280) new=(0,0,1080,2280) req=(1080,2280)0 dur=4 res=0x1 s={true 518689751936} ch=false fn=2
I/ViewRootImpl@eeb58b[MainActivity](14185): updateBoundsLayer: t = android.view.SurfaceControl$Transaction@49c75bf sc = Surface(name=Bounds for - com.eis.lejog.lejog/com.eis.lejog.lejog.MainActivity@1)/@0x9406a03 frame = 2
I/ViewRootImpl@eeb58b[MainActivity](14185): mWNT: t = android.view.SurfaceControl$Transaction@49c75bf fN = 2 android.view.ViewRootImpl.prepareSurfaces:2783 android.view.ViewRootImpl.performTraversals:4029 android.view.ViewRootImpl.doTraversal:2924 
I/ViewRootImpl@eeb58b[MainActivity](14185): mWNT: merge t to BBQ
I/ViewRootImpl@eeb58b[MainActivity](14185): MSG_WINDOW_FOCUS_CHANGED 1 1
D/InputMethodManager(14185): startInputInner - Id : 0
I/InputMethodManager(14185): startInputInner - mService.startInputOrWindowGainedFocus
I/ViewRootImpl@5631977[SignInHubActivity](14185): stopped(true) old=false
D/OpenGLRenderer(14185): setSurface called with nullptr
D/OpenGLRenderer(14185): setSurface() destroyed EGLSurface
D/OpenGLRenderer(14185): destroyEglSurface
I/ViewRootImpl@5631977[SignInHubActivity](14185): dispatchDetachedFromWindow
D/InputTransport(14185): Input channel destroyed: '27c8e0a', fd=156
I/ViewRootImpl@5631977[SignInHubActivity](14185): handleAppVisibility mAppVisible=true visible=false

排查与解决建议

1. 补全SignInScreen的providers配置

代码中注释掉了providers: providers,这会导致Firebase UI无法识别要启用的登录提供商。需要初始化Google登录提供商并传入:

final providers = [
  GoogleProvider(clientId: '你的Google客户端ID'),
];

// 在SignInScreen中启用
return SignInScreen(
  providers: providers,
  headerBuilder: (context, constraints, _) {
    return SignInHeader();
  },
);

注意:客户端ID需从Firebase控制台项目设置 -> 应用 -> 签名证书中获取对应平台的OAuth 2.0客户端ID。

2. 检查依赖版本兼容性

当前firebase_ui_auth与firebase_auth版本可能存在兼容性问题,建议升级到稳定兼容的版本组合:

  • 执行flutter pub outdated查看依赖更新情况
  • 优先添加/升级firebase_core,再同步升级firebase_auth、firebase_ui_auth和firebase_ui_oauth_google到最新稳定版

3. 验证Android端配置

  • 确保android/app/google-services.json是从Firebase控制台下载的最新版本,包名、SHA-1/SHA-256指纹与控制台配置一致
  • 检查android/app/src/main/AndroidManifest.xml是否添加了必要权限和Activity:
<uses-permission android:name="android.permission.INTERNET" />

<activity
    android:name="com.google.android.gms.auth.api.signin.internal.SignInHubActivity"
    android:excludeFromRecents="true" />

4. 捕获详细错误信息

当前仅显示通用错误,建议添加错误捕获获取具体错误码:

return SignInScreen(
  providers: providers,
  headerBuilder: (context, constraints, _) {
    return SignInHeader();
  },
  errorBuilder: (context, exception, stackTrace) {
    print('登录错误详情: $exception');
    return Text('登录失败: ${exception.toString()}');
  },
);

5. 检查Google Play服务版本

测试设备上Google Play服务版本过低会导致登录失败,确保设备安装最新版本的Google Play服务。

6. 验证Firebase控制台配置

  • 确认Firebase控制台中已启用Google登录(Authentication -> 登录方法 -> Google)
  • 检查授权域配置(Web端需确认,移动端重点确保包名和指纹正确)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 10:28:07