Flutter集成Firebase Auth实现Google登录遇阻求助
Flutter Google登录(Firebase Auth)卡在账号选择后无响应的问题解决
问题描述
在Flutter应用中集成Google登录并基于Firebase Auth完成认证时,出现账号选择弹窗消失后流程无响应的问题,代码卡在final googleUser = await googleSignIn.signIn();语句之后,无法继续执行后续逻辑。
相关代码
final GoogleSignIn googleSignIn = GoogleSignIn(); GoogleSignInAccount? currentUser; googleLogin(context, color) async { try { final googleUser = await googleSignIn.signIn(); // loading(context, color); // 无法执行到这里 if (googleUser == null) { Navigator.pop(context); return; } currentUser = googleUser; String? access, id; await googleUser.authentication.then((value) async { if (value != null) { access = value.accessToken; id = value.idToken; } }); UserCredential userCredential = await FirebaseAuth.instance .signInWithCredential( GoogleAuthProvider.credential(idToken: id, accessToken: access)); print('name: ${currentUser!.displayName}'); } catch (e) {}
终端日志
An Observatory debugger and profiler on RMX3363 is available at: http://127.0.0.1:50786/aiMRW_z817c=/ The Flutter DevTools debugger and profiler on RMX3363 is available at: http://127.0.0.1:9101? uri=http://127.0.0.1:50786/aiMRW_z817c=/ V/OplusZoomWindowDecorViewHelper(24796): setLastReportedMergedConfiguration mZoomDisplayHeight: 2400 getDecorView.19495807 D/hw-ProcessState(24796): Binder ioctl to enable oneway spam detection failed: Invalid argument D/SurfaceComposerClient(24796): VRR [FRTC] client handle [bufferId:18446744073709551615 framenumber:0] [ffffffff, ffffffff] D/SurfaceComposerClient(24796): VRR [FRTC] client handle [bufferId:18446744073709551615 framenumber:0] [ffffffff, ffffffff] E/Parcel (24796): Reading a NULL string not supported here. V/OplusZoomWindowDecorViewHelper(24796): setLastReportedMergedConfiguration mZoomDisplayHeight: 2400 getDecorView.200652440 D/SurfaceComposerClient(24796): VRR [FRTC] client handle [bufferId:18446744073709551615 framenumber:0] [ffffffff, ffffffff] V/OplusZoomWindowDecorViewHelper(24796): removeZoomView I/Quality (24796): Skipped: false 1
解决步骤
完善GoogleSignIn初始化配置
默认初始化可能缺少必要参数,尤其是Android平台,建议明确指定scopes和客户端ID(从Firebase控制台获取):final GoogleSignIn googleSignIn = GoogleSignIn( scopes: ['email', 'openid'], clientId: '你的Android客户端ID', );检查Android项目配置
- 确认
android/app/google-services.json文件已正确放置,包名与Firebase控制台注册的一致; - 检查
android/build.gradle和android/app/build.gradle中Firebase依赖及插件配置是否正确,确保com.google.gms:google-services插件已应用; - 确保AndroidManifest.xml添加了互联网权限:
<uses-permission android:name="android.permission.INTERNET" />
- 确认
修复错误处理逻辑
当前代码的catch块无任何输出,无法定位异常原因,修改为:} catch (e) { print('Google登录异常: $e'); }优化authentication获取逻辑
避免混用await和then,改为更清晰的纯await写法:final googleAuth = await googleUser.authentication; final access = googleAuth.accessToken; final id = googleAuth.idToken;检查设备Google服务状态
确保测试设备上的Google Play服务为最新版本,旧版本可能导致登录流程异常。
内容的提问来源于stack exchange,提问作者Diificult Ranger
相关产品推荐
相关产品推荐

