Flutter Firebase Auth Error .createUserWithEmailAndPassword: getGoogleApiForMethod () đã trả về Gms: com.google.firebase.auth.api.internal.zzao@fb1556c
Mở đầu
Đây là một cuộc đấu tranh cho một tốt, trong khi bây giờ và không có gì dường như được làm việc, tôi đã thấy câu trả lời cho câu hỏi tương tự ở đây và ở đây nhưng trước đây đã không rung và sau này đã được đăng trước khi thay đổi gần đây để firebase_auth
và tôi đoán quan trọng nhất là các giải pháp đề xuất đã không hoạt động.
Vấn đề
Vấn đề liên quan đến thiết lập xác thực firebase ban đầu rất đơn giản trong thời gian chập chờn. Tôi đã thiết lập dự án firebase của mình chính xác như nó đề xuất ở đây và sử dụng cái này và cái này để thiết lập firebase_auth
.
Mã gây ra sự cố như sau:
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);
}
}
Khi mã được chạy, lỗi
I / BiChannelGoogleApi (19546): [FirebaseAuth:] getGoogleApiForMethod () đã trả lại Gms: com.google.firebase.auth.api.internal.zzao@fb1556c
Được trả lại.
Câu trả lời này dường như gợi ý rằng lỗi cũng có thể không liên quan, tức là mã vẫn có thể hoạt động. Tôi đặt một điểm ngắt trên dòng UserCredential userCredential = await FirebaseAuth.instance
và bước qua, nó vẫn tiếp tục như vậy .createUserWithEmailAndPassword(
, sau đó khi bước qua một lần nữa sẽ quay trở lại dòng đầu tiên và lỗi được hiển thị. Bước qua bất kỳ bước nào khác sẽ thoát khỏi chức năng hoàn toàn, do đó print(userCredential.user.email);
dòng không bao giờ được thực thi, điều này cho thấy mã không hoạt động. Ngoài ra, print(e);
dòng trong catch
không phải là những gì đang xuất ra lỗi này.
Thông tin khác
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,
),
],
),
),
);
}
}
Ngoài ra, lỗi này đã xuất hiện một vài lần, mặc dù câu trả lời này dường như ngụ ý rằng nó không liên quan gì đến vấn đề của tôi và không phải là dấu hiệu của sự cố.
W / ConnectionTracker (19240): java.lang.IllegalArgumentException: Dịch vụ chưa được đăng ký: 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): tại android.content.ContextWrapper.unbindService (ContextWrapper.java:792) W / ConnectionTracker (19240): tại ci.f (: com .google.android.gms.dynamite_measurementdynamite @ 204217100 @ 20.42.17 (150700-0): 1) W / ConnectionTracker (19240): tại ci.d (: com.google.android.gms.dynamite_measurementdynamite @ 204217100 @ 20.42.17 (150700-0): 2) W / ConnectionTracker (19240): tại lq.D (: com.google.android.gms.dynamite_measurementdynamite @ 204217100 @ 20.42.17 (150700-0): 10) W / ConnectionTracker (19240) : tại lc.a (: com.google.android.gms.dynamite_measurementdynamite @ 204217100 @ 20.42.17 (150700-0): 2) W / ConnectionTracker (19240): tại ee.run (: com.google.android.gms .dynamite_measurementdynamite @ 204217100 @ 20.42.17 (150700-0): 3) W / ConnectionTracker (19240): tại java.util.concurrent.Executor $ RunnableAdapter.call (Executor.java:462) W / ConnectionTracker (19240): tại java.util.concurrent.FutureTask.run (FutureTask.java: 266) W / ConnectionTracker (19240): tại ix.run (: com.google.android.gms.dynamite_measurementdynamite @ 204217100 @ 20.42.17 (150700-0): 6)
BIÊN TẬP
Thay đổi dòng onPressed: () => _registerTestUser(),
trong main.dart
để onPressed: () {_registerTestUser();}
cho kết quả tương tự nhưng thay đổi lỗi một chút, zzao @ fb1556c trở thành zzao @ ebc85e9.
Trả lời
Trong khi không trực tiếp trả lời câu hỏi của bạn và có vẻ mâu thuẫn (mặc dù không khác lắm) với mã mẫu FlutterFire. Đây là mã tôi sử dụng để tạo người dùng (Người dùng loại Firebase_auth) và tôi không thấy lỗi của bạn:
User _user = (await _firebaseAuth.createUserWithEmailAndPassword(
email: email,
password: password,
)).user;
Sau đó, tôi có thể sử dụng các phương thức như await _user.sendEmailVerification();
và truy cập các thuộc tính của nó chẳng hạn như _user.email
không có vấn đề.
Có thể thử điều đó thay vì thông tin đăng nhập.
Tái bút. Vấn đề: W/ConnectionTracker(19240): java.lang.IllegalArgumentException: Service not registered:
bạn đề cập đến đang được theo dõi ở đây và tôi không tin rằng nó liên quan đến mã của bạn:https://github.com/firebase/firebase-android-sdk/issues/1662#issue-638324848
Nó có một số biến thể của mã ngoại lệ nhưng chúng đều có liên quan.
Cập nhật Tôi cũng tin rằng @amit kumar gần như chính xác, bạn đang chạyFirebaseAuth auth = FirebaseAuth.instance;
trước đâyFirebase.initializeApp();
. Tôi tin rằng bạn cần đảm bảo rằng tương lai của mình sẽ hoàn thành trước khi khởi tạo FirebaseAuth. Có thể làm cho mã đã thay đổi của tôi ở trên không liên quan.
Trước khi tôi có thể thấy bạn đã không được khởi tạo firebase
trong main.dart
tập tin một cách chính xác.
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
Bạn đã bỏ lỡ một tuyên bố, hãy thử lại và nếu bạn vẫn gặp sự cố, hãy cho tôi biết
Hãy thử cái này:
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);
}
}
Cũng nên xem xét sử dụng Người dùng thay vì Thông tin đăng nhập của Người dùng:
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);
});
}