Flutter集成Firebase实现Google登录报Expecting binder but got null
Flutter Firebase Google登录模拟器崩溃问题
问题背景
移动应用开发新手使用Flutter结合Firebase Auth实现Google社交登录功能,参考官方文档编写的代码逻辑如下:
main.dart 代码
import 'package:atlas_app/auth/signup.dart'; import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'firebase_options.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); FirebaseAuth.instance.useAuthEmulator('10.0.2.2', 9099); runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key); @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const SignUp()); } }
signin.dart 代码
import 'package:firebase_auth/firebase_auth.dart'; import 'package:flutter/material.dart'; import 'package:flutter_signin_button/button_list.dart'; import 'package:flutter_signin_button/button_view.dart'; import 'package:google_sign_in/google_sign_in.dart'; class SignUp extends StatefulWidget { const SignUp({Key? key}) : super(key: key); @override State<SignUp> createState() => _SignUpState(); } class _SignUpState extends State<SignUp> { FirebaseAuth auth = FirebaseAuth.instance; Future<UserCredential> signInWithGoogle() async { final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn(); final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication; final credential = GoogleAuthProvider.credential( accessToken: googleAuth?.accessToken, idToken: googleAuth?.idToken, ); return await FirebaseAuth.instance.signInWithCredential(credential); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Colors.blue, elevation: 8, title: const Text("Sign Up"), ), body: Container( padding: const EdgeInsets.symmetric(vertical: 20, horizontal: 50), child: Form( child: Column( children: [ SignInButton( Buttons.Google, onPressed: () async => {signInWithGoogle()}, ) ], )), )); } }
故障现象
本以为该段逻辑简单的代码可正常运行,但在搭载Android API 33(arm64架构)的Pixel 4安卓模拟器上点击登录按钮时,应用发生崩溃,调试控制台输出错误信息如下:
W/Parcel (13587): Expecting binder but got null! D/EGL_emulation(13587): app_time_stats: avg=697.35ms min=151.31ms max=1243.39ms count=2 E/JavaBinder(13587): !!! FAILED BINDER TRANSACTION !!! (parcel size = 324) W/GmsClient(13587): IGmsServiceBroker.getService failed W/GmsClient(13587): android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died, but this could also be caused by running out of binder buffe W/GmsClient(13587): at android.os.BinderProxy.transactNative(Native Method) W/GmsClient(13587): at android.os.BinderProxy.transact(BinderProxy.java:584) W/GmsClient(13587): at com.google.android.gms.common.internal.zzac.getService(com.google.android.gms:play-services-basement@@18.0.0:8) W/GmsClient(13587): at com.google.android.gms.common.internal.BaseGmsClient.getRemoteService(com.google.android.gms:play-services-basement@@18.0.0:14) W/GmsClient(13587): at com.google.android.gms.common.api.internal.zabt.run(com.google.android.gms:play-services-base@@18.0.1:7) W/GmsClient(13587): at android.os.Handler.handleCallback(Handler.java:942) W/GmsClient(13587): at android.os.Handler.dispatchMessage(Handler.java:99) W/GmsClient(13587): at android.os.Looper.loopOnce(Looper.java:201) W/GmsClient(13587): at android.os.Looper.loop(Looper.java:288) W/GmsClient(13587): at android.os.HandlerThread.run(HandlerThread.java:67) W/Parcel (13587): Expecting binder but got null! D/EGL_emulation(13587): eglCreateContext: 0xb400006ff0487dd0: maj 3 min 0 rcv 3 D/EGL_emulation(13587): eglCreateContext: 0xb400006ff0489e10: maj 3 min 0 rcv 3 D/HostConnection(13587): createUnique: call D/HostConnection(13587): HostConnection::get() New Host Connection established 0xb400006ff0488f10, tid 13652 D/HostConnection(13587): HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_vulkan_queue_submit_with_commands ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_3_0 D/EGL_emulation(13587): eglMakeCurrent: 0xb400006ff0489e10: ver 3 0 (tinfo 0x720c8be280) (first time) D/HostConnection(13587): createUnique: call D/HostConnection(13587): HostConnection::get() New Host Connection established 0xb400006ff0488010, tid 13651 D/HostConnection(13587): HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_has_shared_slots_host_memory_allocator ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_vulkan_queue_submit_with_commands ANDROID_EMU_sync_buffer_data ANDROID_EMU_read_color_buffer_dma GL_OES_EGL_image_external_essl3 GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_gles_max_version_3_0 D/EGL_emulation(13587): eglMakeCurrent: 0xb400006ff0487dd0: ver 3 0 (tinfo 0x720c8be300) (first time) D/EGL_emulation(13587): eglCreateContext: 0xb400006ff0486f90: maj 3 min 0 rcv 3 E/JavaBinder(13587): !!! FAILED BINDER TRANSACTION !!! (parcel size = 324) W/GmsClient(13587): IGmsServiceBroker.getService failed W/GmsClient(13587): android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died, but this could also be caused by running out of binder buffe ......
故障原因与修复方案
该崩溃和Flutter业务代码逻辑无关,核心是模拟器环境的Google服务异常,按以下优先级排查修复:
- 检查模拟器镜像类型
打开Android Studio的AVD管理器,查看当前使用的Pixel 4模拟器镜像,只有镜像旁标注Play Store图标的版本才内置完整Google服务框架,纯AOSP镜像、仅标注Google APIs的镜像都可能出现服务通信失败问题。如果镜像不对,直接删除后重新选择带Play Store标识的arm64架构API 33/34镜像创建即可。 - 更新Google服务组件
如果已经使用带Play Store的镜像,启动模拟器后打开内置的Play Store,进入「我的应用和游戏」页面,将Google Play服务、Google Play商店全部更新到最新版本,更新完成后冷启动模拟器再测试。 - 排查配置冲突
- 确认Firebase控制台注册Android应用时填入的包名和项目本地包名一致,且已将下载的
google-services.json文件放到Android项目的app模块根目录下 - 确认Gradle配置正确:项目级build.gradle依赖中已添加Google服务插件,app级build.gradle文件末尾已应用
com.google.gms.google-services插件 - 注意:Google第三方登录流程不支持走Firebase Auth本地模拟器,测试Google登录时先注释掉
FirebaseAuth.instance.useAuthEmulator('10.0.2.2', 9099);这行代码,避免服务路由冲突。
- 确认Firebase控制台注册Android应用时填入的包名和项目本地包名一致,且已将下载的
- 代码写法优化(非崩溃原因,仅规范写法)
当前登录按钮的回调写法不规范,箭头函数后无需加花括号,修改为:
也可以使用普通异步函数写法,方便后续加入加载状态、登录结果处理逻辑。SignInButton( Buttons.Google, onPressed: () async => await signInWithGoogle(), )
内容的提问来源于stack exchange,提问作者Takuya.N
相关产品推荐
相关产品推荐

