Flutter集成firebase_auth创建用户时出现getGoogleApiForMethod返回Gms错误
前言
我被这个问题困扰了很长时间,试过的所有方案都没生效。我查过两个相似问题的解答,但第一个是针对非Flutter场景的适配方案,第二个发布于firebase_auth近期版本更新之前,都不适用于当前问题。
问题描述
我在Flutter应用的Firebase Auth极简初始配置场景遇到了这个问题:我完全按照官方文档的指引搭建Firebase项目,结合FlutterFire的总览文档、Auth模块文档完成了firebase_auth的配置。
引发问题的测试代码如下:
void _registerTestUser() async { try { UserCredential userCredential = await FirebaseAuth.instance .createUserWithEmailAndPassword( email: "barry.allen@example.com", password: "SuperSecretPassword!"); print(userCredential.user.email); } on FirebaseAuthException catch (e) { if (e.code == 'weak-password') { print('The password provided is too weak.'); } else if (e.code == 'email-already-in-use') { print('The account already exists for that email.'); } } catch (e) { print(e); } }
运行代码时控制台输出如下日志:
I/BiChannelGoogleApi(19546): [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzao@fb1556c
有开发者提到这类日志可能只是无关提示,代码说不定能正常跑通。但我在调用创建用户的代码行打了断点一步步调试:代码走到.createUserWithEmailAndPassword(这步后,单步执行会直接跳回函数开头,输出上述日志,再单步就直接跳出函数了,后面print(userCredential.user.email);的代码完全不会执行,说明代码根本没正常跑起来;而且这条日志也不是catch块里print(e);语句输出的。
相关配置信息
pubspec.yaml依赖配置
dependencies: flutter: sdk: flutter firebase_core: ^0.5.3 #firebase core flutter sdk firebase_auth: ^0.18.4+1 #firebase authorisation
- 项目级
android/build.gradle配置
dependencies { classpath 'com.android.tools.build:gradle:3.5.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath 'com.google.gms:google-services:4.3.4' // Google Services plugin }
- 应用级
android/app/build.gradle配置
apply plugin: 'com.google.gms.google-services' // Google Services plugin
- 完整
main.dart代码
import 'package:flutter/material.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_auth/firebase_auth.dart'; FirebaseAuth auth = FirebaseAuth.instance; void main() { WidgetsFlutterBinding.ensureInitialized(); runApp(MyApp()); } class MyApp extends StatelessWidget { final Future<FirebaseApp> _initialization = Firebase.initializeApp(); @override Widget build(BuildContext context) { return MaterialApp( home: FutureBuilder( // Initialize FlutterFire: future: _initialization, builder: (context, snapshot) { // Check for errors if (snapshot.hasError) { return Error(); } // Once complete, show your application if (snapshot.connectionState == ConnectionState.done) { return MyHomePage(); } // Otherwise, show something whilst waiting for initialization to complete return Loading(); }, ), ); } } class Loading extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Text('Firebase is loading'), ), ); } } class Error extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Center( child: Text('There has been an error'), ), ); } } class MyHomePage extends StatelessWidget { void _registerTestUser() async { try { UserCredential userCredential = await FirebaseAuth.instance .createUserWithEmailAndPassword( email: "barry.allen@example.com", password: "SuperSecretPassword!"); print(userCredential.user.email); } on FirebaseAuthException catch (e) { if (e.code == 'weak-password') { print('The password provided is too weak.'); } else if (e.code == 'email-already-in-use') { print('The account already exists for that email.'); } } catch (e) { print(e); } } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('FlutterFire Test')), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ RaisedButton( child: Text('Register a test user'), onPressed: () => _registerTestUser(), color: Colors.blue, ), ], ), ), ); } }
除此之外控制台还时不时输出下面的警告日志,有开发者提到这类日志和当前问题没关系,不属于故障提示:
W/ConnectionTracker(19240): java.lang.IllegalArgumentException: Service not registered: lp@fb1556c
W/ConnectionTracker(19240): at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1729)
W/ConnectionTracker(19240): at android.app.ContextImpl.unbindService(ContextImpl.java:1874)
W/ConnectionTracker(19240): at android.content.ContextWrapper.unbindService(ContextWrapper.java:792)
W/ConnectionTracker(19240): at ci.f(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):1)
W/ConnectionTracker(19240): at ci.d(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):2)
W/ConnectionTracker(19240): at lq.D(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):10)
W/ConnectionTracker(19240): at lc.a(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):2)
W/ConnectionTracker(19240): at ee.run(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):3)
W/ConnectionTracker(19240): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
W/ConnectionTracker(19240): at java.util.concurrent.FutureTask.run(FutureTask.java:266)
W/ConnectionTracker(19240): at ix.run(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):6)
补充说明
我把main.dart里按钮的onPressed: () => _registerTestUser(),改为onPressed: () {_registerTestUser();}后问题还是存在,只是日志中的对象哈希值从zzao@fb1556c变成了zzao@ebc85e9。
内容的提问来源于Stack Exchange,提问作者Tom Ryan

