flutter_smart_auth
flutter_smart_auth copied to clipboard
Application crashes when SMS is received.
Here, I am using Firebase OTP. When I try to use autofill with the userConsent API . Here is the Details Log when app crash.
I/PlayCore(17213): UID: [11666] PID: [17213] IntegrityService : reportBinderDeath
I/PlayCore(17213): UID: [11666] PID: [17213] IntegrityService : IntegrityService : Binder has died.
D/AndroidRuntime(17213): Shutting down VM
E/AndroidRuntime(17213): FATAL EXCEPTION: main
E/AndroidRuntime(17213): Process: <applicationId>, PID: 17213
E/AndroidRuntime(17213): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.google.android.gms.auth.api.phone.SMS_RETRIEVED flg=0x200010 pkg=<applicationId>has extras) } in com.google.android.gms.internal.firebase-auth-api.zzaby@81dcfe5
E/AndroidRuntime(17213): at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1708)
E/AndroidRuntime(17213): at android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run(Unknown Source:2)
E/AndroidRuntime(17213): at android.os.Handler.handleCallback(Handler.java:938)
E/AndroidRuntime(17213): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(17213): at android.os.Looper.loopOnce(Looper.java:210)
E/AndroidRuntime(17213): at android.os.Looper.loop(Looper.java:299)
E/AndroidRuntime(17213): at android.app.ActivityThread.main(ActivityThread.java:8319)
E/AndroidRuntime(17213): at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(17213): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
E/AndroidRuntime(17213): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1038)
E/AndroidRuntime(17213): Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
E/AndroidRuntime(17213): at java.util.regex.Matcher.reset(Matcher.java:256)
E/AndroidRuntime(17213): at java.util.regex.Matcher.
If you are using FIrebaseAuth you don't need to use smart_auth to get the SMS code. You just need to handle verificationCompleted.
await FirebaseAuth.instance.verifyPhoneNumber(
verificationCompleted: (PhoneAuthCredential credential) {
pinController.setText(credential.smsCode);
},
verificationFailed: (FirebaseAuthException e) {},
codeSent: (String verificationId, int? resendToken) {},
codeAutoRetrievalTimeout: (String verificationId) {},
);
I receive messages on my phone, but the auto-fill feature is not working. Here is my code for this issue. Even after receiving a message, the controller value remains empty. Do I need to update something in this code, or is there any configuration I have to adjust in the project? Here is my sample code:
import 'dart:developer';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:pinput/pinput.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final scaffoldKey = GlobalKey();
final TextEditingController controller = TextEditingController();
sendOtp() async {
FirebaseAuth auth = FirebaseAuth.instance;
await auth.verifyPhoneNumber(
phoneNumber: "+9779860718700",
verificationCompleted: (PhoneAuthCredential credential) async {
controller.setText(credential.smsCode ?? "");
},
verificationFailed: (FirebaseAuthException e) {
log('The provided phone number is not valid.${e.code}');
},
codeSent: (String verificationId, int? resendToken) {},
codeAutoRetrievalTimeout: (String verificationId) {},
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
key: scaffoldKey,
appBar: AppBar(
title: const Text('Plugin example app'),
actions: [
IconButton(
onPressed: () async {
controller.setText("123456");
},
icon: const Icon(Icons.home),
),
IconButton(
onPressed: () async {
log("This is log ${controller.text}");
},
icon: const Icon(Icons.abc),
),
IconButton(
onPressed: () => sendOtp(),
icon: const Icon(Icons.send),
)
],
),
body: Center(
child: Padding(
padding: const EdgeInsets.all(40.0),
child: Pinput(
length: 6,
androidSmsAutofillMethod: AndroidSmsAutofillMethod.none,
// androidSmsAutofillMethod: AndroidSmsAutofillMethod.smsUserConsentApi,
controller: controller,
),
),
),
),
);
}
}
Is the verificationCompleted method called at all?
After invoking the function to send the OTP, the reCAPTCHA check occurs in Chrome. Upon successful verification, the page returns to the app. Subsequently, the SMS is received, but the 'verificationCompleted' method doesn't trigger. Is there an error in my code, or do I need to make any updates? Could you please review my code and correct it if necessary? Thank you.
The code looks good, but I don't understand why firebase_auth is redirecting you to the reCAPTCHA.