Read OTP from SMS in flutter

Updated 28 March 2024

Save

In this comprehensive guide, we’ll explore the process of integrating incoming message reading capabilities into Flutter apps, we will look how to read OTP from sms and auto fill the OTP field. From sending sms in emulator to message retrieval and processing, we’ll cover all the essential steps to seamlessly implement this functionality.

Need for SMS Reading
Imagine a scenario where a user needs to verify their identity using a one-time password (OTP) sent via SMS. Traditionally, users would have to switch between their messaging app and the authentication screen, manually entering the OTP. This process is not only cumbersome but also prone to errors. By enabling Flutter apps to read incoming messages, developers can streamline such interactions, offering users a more intuitive and efficient experience.

Before we dive into the code, you can also check our Flutter app development company page.

Implementation

1. Firstly, create a new project and add dependency (sms_autofill) into pubspec.yaml file as

2. Create a basic User interface to show the OTP and submit button as below code

In above code we have created UI view with the help of :

format the textFormField input in Flutter

3. Create a method (listenOtp) to register a listener that listen for incoming sms and call from initState as :

listenOtp method initialize a listener that listen for incoming OTP :

getAppSignature : It will return your app signature in a String format, you have to add it at the end of your sms while sending sms to this device.

listenForCode : create and register a listener.

4. Unregister the listener in dispose method with (unregisterListener) :

Complete code

Complete code for the implementation

Output of the example code

For sending the OTP in emulator you can use the emulator control settings options, choose phone option from left menu and then write your message click on send message button as shown below :

Conclusion

Thanks for reading this article ❤️

I hope this blog will help you to learn about how to read OTP from sms in Flutter and you will be able to implement it. For more updates, make sure to keep following Mobikul Blogs to learn more about mobile app development.

Happy Learning ✍️

Other blogs you may like…

Firebase phone OTP authentication

SMS retrieval API in android

Reference

https://pub.dev/packages/sms_autofill

author
. . .

Leave a Comment

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


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home