这已经很长时间了,似乎没有任何效果,我看到了类似问题here和here的答案,但前者没有颤抖,后者已发布在 recent changes 到 firebase_auth
之前,我想最重要的是提出的解决方案没有奏效。
该问题涉及在 flutter 中对 firebase 身份验证进行非常非常简单的初始设置。我已经完全按照 here 的建议设置了我的 firebase 项目,并使用 this 和 this 设置了 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
被退回。
This answer 似乎表明该错误可能无关紧要,即代码无论如何都可以工作。我在行 UserCredential userCredential = await FirebaseAuth.instance
上放置了一个断点并跨过,它继续 .createUserWithEmailAndPassword(
,然后再次跨过回到第一行并显示错误。跳过函数的任何进一步中断,因此永远不会执行 print(userCredential.user.email);
行,这表明代码不起作用。此外,print(e);
中的 catch
行不是输出此错误的内容。
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,
),
],
),
),
);
}
}
这个错误也出现了几次,虽然 this answer 似乎暗示它与我的问题无关,并不表示有问题。
<块引用>W/ConnectionTracker(19240):java.lang.IllegalArgumentException:服务未注册:lp@fb1556c W/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): at 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.run(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):3) W/ConnectionTracker(19240):在 java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462) W/ConnectionTracker(19240):在 java.util.concurrent.FutureTask.run(FutureTask.java:266) W/ConnectionTracker(19240):在 ix.run(:com.google.android.gms.dynamite_measurementdynamite@204217100@20.42.17 (150700-0):6)
将 onPressed: () => _registerTestUser(),
中的行 main.dart
更改为 onPressed: () {_registerTestUser();}
给出了相同的结果,但稍微改变了错误,zzao@fb1556c 变成了 zzao@ebc85e9。
答案 0 :(得分:0)
首先,我看到您没有正确初始化 firebase
文件中的 main.dart
。
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
您错过了一个语句再试一次,如果仍有问题,请告诉我
答案 1 :(得分:0)
虽然没有直接回答您的问题,并且显然与 FlutterFire 示例代码不一致(尽管与此没有太大区别)。这是我用来创建用户(Firebase_auth 类型用户)的代码,但我没有看到您的错误:
User _user = (await _firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
)).user;
然后我可以使用 await _user.sendEmailVerification();
等方法并访问其属性(例如 _user.email
)而不会出现问题。
也许可以试试这个,而不是凭证。
附注。问题:W/ConnectionTracker(19240): java.lang.IllegalArgumentException: Service not registered:
您提到的正在此处被跟踪,我认为这与您的代码无关:https://github.com/firebase/firebase-android-sdk/issues/1662#issue-638324848
它有几种异常代码的变体,但它们都是相关的。
更新
我也相信@amit kumar 几乎是正确的,您在 FirebaseAuth auth = FirebaseAuth.instance;
之前运行 Firebase.initializeApp();
。我相信,在实例化 FirebaseAuth 之前,您需要确保您的未来完成。可能会使我上面更改的代码变得无关紧要。
答案 2 :(得分:0)
试试这个:
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);
}
}
答案 3 :(得分:0)
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);
});
}