π Introduction
Auto-filling OTP (One-Time Password) has become a standard feature in modern mobile apps. It improves user experience by eliminating the need to manually type verification codes.
In this guide, youβll learn how auto OTP detection works in Flutter, along with step-by-step implementation, best packages, and SEO-friendly best practices.
π What is OTP Auto Fill?
OTP Auto Fill is a feature that automatically reads the verification code sent via SMS and fills it into the input field.
This is commonly used in:
- Login/Signup authentication
- Phone number verification
- Payment verification systems
βοΈ How Auto Fill OTP Works
Auto OTP detection relies on:
- SMS Retriever API (Android)
- Secure Code AutoFill (iOS)
π Behind the scenes:
- User enters phone number
- App requests OTP from server
- Server sends SMS with OTP
- App listens for incoming SMS
- OTP is extracted and auto-filled
π¦ Best Flutter Packages for OTP Auto Fill
1. sms_autofill
- Most popular package
- Uses SMS Retriever API
- No SMS permission required
2. otp_autofill
- Advanced control
- Supports multiple methods
π Recommended: sms_autofill
π Step-by-Step Implementation
β Step 1: Add Dependency
Add this to your pubspec.yaml:
dependencies:
sms_autofill: ^2.3.0
β Step 2: Import Package
import 'package:sms_autofill/sms_autofill.dart';
β Step 3: Create OTP Text Field
class OTPPage extends StatefulWidget {
@override
_OTPPageState createState() => _OTPPageState();
}class _OTPPageState extends State<OTPPage> with CodeAutoFill {
String code = ""; @override
void initState() {
super.initState();
listenForCode();
} @override
void codeUpdated() {
setState(() {
code = this.code!;
});
} @override
void dispose() {
cancel();
super.dispose();
} @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("OTP Verification")),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
PinFieldAutoFill(
codeLength: 6,
onCodeChanged: (code) {},
onCodeSubmitted: (code) {},
),
Text("OTP: $code"),
],
),
);
}
}
π© SMS Format for Auto Detection (Very Important)
For auto-fill to work, your SMS must follow this format:
Your OTP code is 123456
FA+9qCX9VSu
π₯ Key Points:
- Last line = App hash signature
- Required for Android SMS Retriever API
- Without this, auto-fill may NOT work
π How to Get App Signature Hash
Run this command:
flutter pub run sms_autofill:getAppSignature
π± iOS Configuration
For iOS:
- No extra setup needed
- Just ensure SMS contains OTP like:
Your OTP is 123456
- iOS automatically detects and suggests OTP in keyboard
π― Best Practices
- Use 4β6 digit OTP
- Keep OTP expiry short (30β60 seconds)
- Never store OTP locally
- Always verify OTP on server
β Common Issues & Fixes
π« OTP Not Auto Filling?
β Check:
- Correct SMS format
- App hash included
- Real device (not emulator)
- SMS received on same device
π Benefits of Auto OTP Fill
- Faster login/signup
- Better UX
- Reduced user errors
- Higher conversion rate
π§ Pro Tips
- Combine with Firebase Authentication
- Add resend OTP timer
- Use animation for OTP fields
- Mask phone numbers for security
π Conclusion
Auto OTP fill in Flutter is easy to implement using packages like sms_autofill. By following proper SMS format and setup, you can create a smooth and secure authentication experience for your users.
0 Comments