Read Inbox Messages - Flutter SMS Retriever

Last updated Feb 23, 2022

In this post we are going to learn how to read all Inbox Messages from Phone messages box in Flutter. To read SMS we are going to use SMS plugin. In the second part of this tutorial we will cover Search Inbox messages based on Number, messages.

The SMS plugin support below features

  • SMS Receiver, Sender, Delivery, Query, Thread
  • Contact
  • Contact Photo (full size, thumbnail)
  • User profile (basic info)

You can aslo read how to Read Phone Contacts in Flutter Application

 

Let's get started

Step 1: Create flutter application

Step 2: Add dependencies

Now to read SMS we need to add our plugin. Let add sms dependencies in pubspec.yaml file and run flutter pub get on terminal

dev_dependencies:
  flutter_test:
    sdk: flutter
  sms: ^0.2.4

 

After adding the plugin save the file and run command "flutter pub get" to include plugin packages in current project

 

Step 3: Create dart file and add below code. Here we will add our read SMS login using  SMS plugin.

before going to check the complete code read about what is SMS plugin to read SMS,

 

import 'package:sms/sms.dart';

  

To handle the SMS we need to create instance of SmsQuery

SmsQuery query = new SmsQuery();

This SmsQuery class has method getAllSms() which will returns all SMS in your phone inbox as a list of SmsMessage.

await query.getAllSms

Here we added await keyword before the query, which means that query will execute asynchronously with returned data.

The below code we are using the Future builder to read sms from Inbox by query.getAllSms method

This will return list of all messages from inbox.

 

class MyInbox extends StatefulWidget{
  @override
  State createState() {
    // TODO: implement createState
    return MyInboxState();
  }

}

class MyInboxState extends State{
  SmsQuery query = new SmsQuery();
  List messages=new List();
  @override
 initState()  {
    // TODO: implement initState
    super.initState();

  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("SMS Inbox"),
        backgroundColor: Colors.pink,
      ),
      body: FutureBuilder(
        future: fetchSMS() ,
        builder: (context, snapshot)  {

        return ListView.separated(
            separatorBuilder: (context, index) => Divider(
              color: Colors.black,
            ),
            itemCount: messages.length,
            itemBuilder: (context,index){
          return Padding(
            padding: const EdgeInsets.all(8.0),
            child: ListTile(
              leading: Icon(Icons.markunread,color: Colors.pink,),
              title: Text(messages[index].address),
              subtitle: Text(messages[index].body,maxLines:2,style: TextStyle(),),
            ),
          );
        });
      },)
    );
  }

  fetchSMS()
  async {
    messages = await query.getAllSms;
  }
}


 

Step 4: Update main dart file

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: MyInbox(),
    );
  }
}

 

Step 5: Run application

Read SMS in flutter

Note: Add SMS Permissions in Manifest file

Related Examples

Flutter AUTO READ OTP

 

Conclusion: In this flutter example we covered how to read SMS from phone inbox using SMS plugin

Tags: How to Read SMS, Read Inbox Messages, Flutter SMS Retriever

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

11861 Views