У меня проблема с firebase в моем приложении. Я делаю OTP с firebase в моем приложении. Но возникла такая проблема при попытке отправить номер телефона:
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist.
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@19.3.0:218)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at io.flutter.plugins.firebaseauth.FirebaseAuthPlugin.getAuth(FirebaseAuthPlugin.java:80)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at io.flutter.plugins.firebaseauth.FirebaseAuthPlugin.onMethodCall(FirebaseAuthPlugin.java:160)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:231)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:93)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:642)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at android.app.ActivityThread.main(ActivityThread.java:7356)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
E/MethodChannel#plugins.flutter.io/firebase_auth(19880): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/flutter (19880): isValid - true
I/flutter (19880): mobiel +90151515
E/flutter (19880): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, FirebaseApp with name [DEFAULT] doesn't exist. , null)
Я добавил google-сервисы. json в android / app. И я добавил эти зависимости в android / build.gradle, а также попытался изменить разные версии, но приложение работало только с 4.1.0:
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// Add the google services classpath
classpath 'com.google.gms:google-services:4.1.0'
}
В конце в android / app / build.gradle также добавлено применить плагин:
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.google.firebase:firebase-core:17.0.0'
}
apply plugin: 'com.google.gms.google-services'
Вот мой исходный код:
void _onVerifyCode() async {
setState(() {
isCodeSent = true;
});
final PhoneVerificationCompleted verificationCompleted =
(AuthCredential phoneAuthCredential) {
_firebaseAuth
.signInWithCredential(phoneAuthCredential)
.then((AuthResult value) {
if (value.user != null) {
// Handle loogged in state
print(value.user.phoneNumber);
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => HomeWidget(
user: value.user,
),
),
(Route<dynamic> route) => false);
} else {
showToast("Error validating OTP, try again", Colors.red);
}
}).catchError((error) {
showToast("Try again in sometime", Colors.red);
});
};
final PhoneVerificationFailed verificationFailed =
(AuthException authException) {
showToast(authException.message, Colors.red);
setState(() {
isCodeSent = false;
});
};
final PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async {
_verificationId = verificationId;
setState(() {
_verificationId = verificationId;
});
};
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) {
_verificationId = verificationId;
setState(() {
_verificationId = verificationId;
});
};
// TODO: Change country code
await _firebaseAuth.verifyPhoneNumber(
phoneNumber: "+90${widget.mobileNumber}",
timeout: const Duration(seconds: 60),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
}
void _onFormSubmitted() async {
AuthCredential _authCredential = await PhoneAuthProvider.getCredential(
verificationId: _verificationId, smsCode: _pinEditingController.text);
_firebaseAuth
.signInWithCredential(_authCredential)
.then((AuthResult value) {
if (value.user != null) {
// Handle loogged in state
print(value.user.phoneNumber);
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => HomeWidget(
user: value.user,
),
),
(Route<dynamic> route) => false);
} else {
showToast("Error validating OTP, try again", Colors.red);
}
}).catchError((error) {
showToast("Something went wrong", Colors.red);
});
}
Очистка флаттера тоже не сработала!