๐Ÿ” 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?

Advertisement

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:

  1. User enters phone number
  2. App requests OTP from server
  3. Server sends SMS with OTP
  4. App listens for incoming SMS
  5. 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.