Are you a Flutter developer? Do you integrate a sign-up or sign-in for the authenticate user process? Once you sign up or sign in with your mobile number, at that time you will get OTP in the SMS on your mobile device. To verify that OTP, you need to copy and paste it in your text field or pinbox. Sometimes you need to go to the messaging app and see that OTP and enter it in the text field or pinbox to verify. It’s an irritative user experience while otp verify process.
In this article, we will learn how to read OTP from SMS and enable OTP autofill in Flutter. It simplifies the verification process for users.
Table of Contents
Let’s start How to Read OTP from SMS Autofill in Flutter
Step 1: Create a new Project or open an existing one
- Open Android Studio.
- Click on New Flutter Project.
- Make sure you have selected your Flutter SDK Path. Click Next
- Enter the Project Name, Project Location (where you need to put the project),
- Select the project type (Default Application).
- Enter Organization
- Select Android and iOS languages (default: Kotlin and Swift).
- Select Platforms (On which platform you want to run the project).
- Click on Finish button.
- Now wait for the project to finish building.
Step 2: Adding the sms_autofill Package in the pubspec.yaml
- Open the pubspec.yaml file from the left panel of the project directory.
- Add the sms_autofill package under dependencies.
dependencies:
sms_autofill: ^2.4.0
- Click on Pub get to retrieve packages in the project.
Step 3: Modify AndroidManifest.xml (Enabling Auto-Read OTP)
- Add the RECEIVE_SMS permission to the Android Manifest file. Go to android/app/src/main/AndroidManifest.xml
- Add RECEIVE_SMS permission before <application> tag.
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-feature android:name="android.hardware.telephony"/>
Step 4: Import the Package
In your Dart file, import the sms_autofill
package.
import 'package:sms_autofill/sms_autofill.dart';
Step 5: Initialize SMS Autofill
In your main Dart file, initialize the SMS autofill plugin.
void main() {
WidgetsFlutterBinding.ensureInitialized();
SmsAutoFill().listenForCode;
runApp(MyApp());
}
Step 6: Implement OTP Autofill
Create a widget that uses the OTP autofill functionality.
class VerifyOtp extends StatefulWidget {
@override
_VerifyOtpState createState() => _VerifyOtpState();
}
class _VerifyOtpState extends State<VerifyOtp> with CodeAutoFill {
String? otpCode;
@override
void codeUpdated() {
setState(() {
otpCode = code;
});
}
@override
void initState() {
super.initState();
listenForCode();
}
@override
void dispose() {
cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
PinFieldAutoFill(
codeLength: 6,
autoFocus: true,
currentCode: otpCode,
onCodeChanged: (code) {
setState(() {
otpCode = code;
// You can handle OTP verification here automatically.
});
},
),
ElevatedButton(
onPressed: () {
// Handle OTP submission or call api for sending OTP
},
child: Text('Submit'),
),
],
);
}
}
For iOS, this package is not needed as the SMS autofill is provided by default.
You can use two types of widgets to use OTP. (1) PinFieldAutoFill (2) TextFieldPinAutoFill
PinFieldAutoFill(
decoration: // Use of UnderlineDecoration, BoxLooseDecoration or BoxTightDecoration,
currentCode: // prefill with a code
onCodeSubmitted: //code submitted callback
onCodeChanged: //code changed callback
codeLength: //code length
),
TextFieldPinAutoFill(
decoration: // basic InputDecoration
currentCode: // prefill with a code
onCodeSubmitted: //code submitted callback
onCodeChanged: //code changed callback
codeLength: //code length
),
Important Note: Make sure that the SMS you will get should contain ‘<#>’ at the beginning of the SMS in your Android phone and your app signature key at the end of the SMS. It should be like that.
<#> Your otp code is 123456 appSignature.
Important Notes: Make sure you have to set your appSignature in your custom message. You will get two different app signatures for debug and release build.
There are various methods to generate an app signature key; you can use the below code to get an app signature key.
@override
void initState() {
listenForCode();
SmsAutoFill().getAppSignature.then((signature) {
appSignature = signature;
print("App Signature is: $appSignature");
});
super.initState();
}
Conclusion
Reading OTP from SMS and enabling OTP autofill in Flutter can greatly improve the user experience. By following the steps in this article, you can easily implement SMS autofill and auto-read OTP in Android for your Flutter apps.
Frequently Asked Questions(FAQs)
How to read OTP automatically in Flutter?
To read OTP from SMS in Flutter, you need to integrate the SMS_autofill package in your code.
How to listen incoming SMS in Flutter?
You need to implement “await SmsAutoFill().listenForCode();” code in initState() to listen SMS using the sms_autofill package in Flutter.
How do I set my OTP to auto read?
If you want to set your OTP autoread in your Flutter project, then you need to integrate the SMS_autofill package in your project. I explained step-by-step information about it.
How to use sms_autofill?
To use sms_autofill in your Flutter project, you need to add the sms_autofill package in your YAML file. I provided a step-by-step guide about it.
How to auto read OTP in Android programmatically?
To auto-read OTP in Android programmatically, you need to request the RECEIVE_SMS
and READ_SMS permissions in your AndroidManifest.xml
file. You can use the SmsVerifyCatcher library for native Android.
I’m a Flutter and iOS developer, entrepreneur, and owner of fluttertutorialhub.com. I live in India, and I love to write tutorials and tips that can help other developers. I am a big fan of Flutter, FlutterFlow, iOS, Android, mobile application development, and dart programming languages from the early stages. I believe in hard work and consistency.
Hire me on Upwork: https://www.upwork.com/freelancers/~01b4147994e5007ffc