无法在 Flutter 上使用 Firebase 进行谷歌登录; “期待活页夹,但为空!”

我是移动应用程序的新手,并使用颤振通过 firebase auth 实现谷歌社交登录。我检查了官方资源并实现了如下登录逻辑;

主要.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);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const SignUp());
  }
}

登录.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 {
  // Trigger the authentication flow
  final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();

  // Obtain the auth details from the request
  final GoogleSignInAuthentication? googleAuth = await googleUser?.authentication;

  // Create a new credential
  final credential = GoogleAuthProvider.credential(
    accessToken: googleAuth?.accessToken,
    idToken: googleAuth?.idToken,
  );

  // Once signed in, return the UserCredential
  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 模拟器(带有 android API 33、arm64 的 Pixel4)上的登录按钮时,我的应用程序崩溃了,调试控制台说;

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
......

我一直在解决这个问题,但还没有解决问题的线索。任何帮助将不胜感激!

stack overflow Cannot proceed google login with firebase on flutter; "Expecting binder but got null!"
原文答案

答案:

作者头像

here 所述,该问题仅在设备未安装 PlayStore 时出现。

我也看到了这个错误,但似乎没有人知道解决方案。

作者头像

在 main.dart 添加这个

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const Foo());
}
作者头像

我在这里找到了解决方案。错误来自模拟器,将模拟器版本从 31.3.10 更改为 31.2.9 https://youtu.be/qo3SbdbXFos