πŸ” 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:

  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.

Categories: Flutter

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *