How To Read Sms In Flutter With Flutter Sms Plugin



Welcome back InstaReaders! In this tutorial, I will show you how to read and retrieve all SMS in Flutter.


The app like TrueCaller, which has all the features of call, contacts and SMS is great. I could not afford the premium and ads have always been annoying to me. 

Also, I am insecure about privacy too. So when I came across this awesome flutter plugin, I was excited to make my own app and not worry about others.

You can check my other flutter tutorials in link below:

In this tutorial, I will focus mainly on retrieving SMS in Flutter with SMS plugin. As and when I explore other feature of sending/receiving SMS, calls and contacts. 


I will write the post and link it below for the reference. If I achieve my dream of the app, I will upload it and share with you guys.

So let us start our tutorial.

Creating New Flutter Project

As usual we will start by creating new flutter project. We will call our project as “InstaInbox“.

flutter create InstaInbox

The SMS Plugin And Configuration

Next we will use one of the great and simple to implement plugin from Flutter’s repository. The plugin is called “sms“.

As per their official page, the plugin currently works for the Android devices only. Even in their road map, I could not find if they will be including iOS devices anytime soon.

Following are the currently available feature in the plugin:

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

Here are list of features that is NOT on road-map.

  • MMS Receiver, Sender, Delivery, Query
  • Multi Sim Card

Do you know better plugin? Let me know in Comment section, I will surely check it out.

Writing The SMS Retrieving Code

We will begin by including the sms plugin the the pubsec.yaml file.

dependencies:

  sms: ^0.2.4

After adding the plugin, you can save the file. The editor should automatically run pub get or you can run it manually by entering “flutter pub get” in your terminal.

Now we will get into coding the main.dart file. We will use this file to call our other file, where the logic for importing SMS is coded.

import 'package:InstaInbox/my_inbox.dart';

import 'package:flutter/material.dart';

 

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(),

    );

  }

}

From the above code, you can make out that we will make a new file called my_inbox.dart under the lib directory.

Before getting into all the code, I would like to explain to you some of the instances and methods from a plugin that we will use.

We will be required to create an instance of the SmsQuery.

SmsQuery query = new SmsQuery();

The method getAllSms from the SmsQuery class returns all the SMS in your inbox as a list of SmsMessage.

List<smsmessage> messages = await query.getAllSms;

Notethe use of await keyword means that getAllSms is resolved asynchronously and a Future is retorned.

So here is our full code looks like.

import 'package:flutter/material.dart';

import 'package:sms/sms.dart';

 

class MyInbox extends StatefulWidget {

  @override

  State createState() {

    return MyInboxState();

  }

}

 

class MyInboxState extends State {

  SmsQuery query = new SmsQuery();

  List<SmsMessage> messages = new List<SmsMessage>();

  @override

  initState() {

    super.initState();

  }

 

  @override

  Widget build(BuildContext context) {

    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;

  }

}

That is all folks!

You can save your flutter project and run it.

If you have any question or if you have faced any issues, let me know in comment section.

 

Previous Post Next Post