Flutter Firebase Auth错误.createUserWithEmailAndPassword:getGo



序言

这已经是一场斗争了很长一段时间,似乎什么都不起作用,我在这里和这里看到了类似问题的答案,但前者并不激动,后者是在firebase_auth最近更改之前发布的,我想最重要的是,提出的解决方案没有起作用。

问题

这个问题涉及在flutter中进行非常简单的firebase身份验证的初始设置。我已经按照这里的建议建立了我的firebase项目,并用这个和这个来建立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((返回Gms:com.google.firebase.auth.api.internal.zzao@fb1556c

返回。

这个答案似乎表明错误很可能是无关的,即代码无论如何都可能工作。我在UserCredential userCredential = await FirebaseAuth.instance行上放了一个断点,然后跨过去,它继续到.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.IollegalArgumentException:服务未注册:lp@fb1556cW/ConnectionTracker(19240(:在android.app.LoadedApk.forgetServiceDispatcher上(LoadedApk.java:1729(W/ConnectionTracker(19240(:在android.app.ContextImpl.unbindService(ContextImpl.java:1874(W/ConnectionTracker(19240(:位于android.content.ContextWrapper.unbindService(ContextWrapper.java:792(W/ConnectionTracker(19240(:在ci.f(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17(150700-0(:1(W/ConnectionTracker(19240(:在ci.d(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17(150700-0(:2(W/ConnectionTracker(19240(:在lq。D(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17(150700-0(:10(W/ConnectionTracker(19240(:在lc.a(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17(150700-0(:2(W/ConnectionTracker(19240(:在ee运行时(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17(150700-0(:3(W/ConnectionTracker(19240(:位于java.util.concurrent.Executors$RunnableAdapter.call(Executitors.java:462(W/ConnectionTracker(19240(:位于java.util.concurrent.FFutureTask.run(FutureTask.java:266(W/ConnectionTracker(19240(:在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.

虽然没有直接回答您的问题,而且显然与FlutterFire示例代码不一致(尽管没有很大不同(。以下是我用来创建用户的代码(Firebase_auth-TypeUser(,我没有看到您的错误:

User _user = (await _firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
)).user;

然后,我可以使用诸如await _user.sendEmailVerification();之类的方法,并毫无问题地访问其诸如_user.email之类的属性。

也许试试这个而不是证书。

PS。问题:W/ConnectionTracker(19240): java.lang.IllegalArgumentException: Service not registered:你提到的正在这里被跟踪,我不认为它与你的代码有关:https://github.com/firebase/firebase-android-sdk/issues/1662#issue-638324848

它有几种异常代码变体,但它们都是相关的。

更新我也相信@amit-kumar几乎是正确的,您在Firebase.initializeApp();之前运行FirebaseAuth auth = FirebaseAuth.instance;。我相信,在实例化FirebaseAuth之前,您需要确保您的未来完成。可能会使我上面更改的代码变得无关紧要。

首先,正如我所看到的,您没有正确初始化main.dart文件中的firebase

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}

你错过了一个语句,再试一次,如果你仍然有问题,请告诉我

尝试一下:

void _registerTestUser() async {
try {
await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: "barry.allen@example.com",
password: "SuperSecretPassword!")
.then((userCredential) => 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);
}
}

还可以考虑使用User而不是UserCredential:

void _registerTestUser() async {
try {
final User user = (await FirebaseAuth.instance
.createUserWithEmailAndPassword(
email: "barry.allen@example.com",
password: "SuperSecretPassword!"))
.user;
print(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);
}
}
final FirebaseAuth _auth = FirebaseAuth.instance;
registerUser()async{
_auth.createUserWithEmailAndPassword(
email:"barry.allen@example.com",
password: "SuperSecretPassword!",
).then((result){
User user = result.user;
}).catchError((e) {
print(e);
});
}

最新更新