I recently been trying to develop a Flutter Project which can read all the SMS from Android phone ( i have android phone ).
and I am using the below code
import 'package:flutter/material.dart';
import 'package:flutter_sms_inbox/flutter_sms_inbox.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final SmsQuery _query = SmsQuery();
List<SmsMessage> _messages = [];
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter SMS Inbox App',
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: Scaffold(
appBar: AppBar(
title: const Text('SMS Inbox Example'),
),
body: Container(
padding: const EdgeInsets.all(10.0),
child: _messages.isNotEmpty
? _MessagesListView(
messages: _messages,
)
: Center(
child: Text(
'No messages to show.\n Tap refresh button...',
style: Theme.of(context).textTheme.headlineSmall,
textAlign: TextAlign.center,
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
var permission = await Permission.sms.status;
if (permission.isGranted) {
final messages = await _query.querySms(
kinds: [
SmsQueryKind.inbox,
SmsQueryKind.sent,
],
// address: '+254712345789',
count: 10,
);
debugPrint('sms inbox messages: ${messages.length}');
setState(() => _messages = messages);
} else {
await Permission.sms.request();
}
},
child: const Icon(Icons.refresh),
),
),
);
}
}
class _MessagesListView extends StatelessWidget {
const _MessagesListView({
Key? key,
required this.messages,
}) : super(key: key);
final List<SmsMessage> messages;
@override
Widget build(BuildContext context) {
return ListView.builder(
shrinkWrap: true,
itemCount: messages.length,
itemBuilder: (BuildContext context, int i) {
var message = messages[i];
return ListTile(
title: Text('${message.sender} [${message.date}]'),
subtitle: Text('${message.body}'),
);
},
);
}
}
but still it is not able to show the sms from my phone instead it is showing me error
E/flutter ( 7900): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=7900, uid=10284 requires android.permission.READ_SMS, or grantUriPermission(), null, java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=7900, uid=10284 requires android.permission.READ_SMS, or grantUriPermission()
I am using this dependancy flutter_sms_inbox: ^1.0.3
and I am also using the code given at the example tab of this dependancy
The error indicates that your application doesn't have the necessary permissions to read SMS messages from the Android device
You need to add
<uses-permission android:name="android.permission.READ_SMS"/>
to your flutter project/android/AndroidManifest.xml
You also need to ensure READ_SMS is granted before requesting
Sample
import 'package:flutter/material.dart';
import 'package:flutter_sms_inbox/flutter_sms_inbox.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final SmsQuery _query = SmsQuery();
List<SmsMessage> _messages = [];
@override
void initState() {
super.initState();
// Request SMS permission when the app starts
_requestSmsPermission();
}
// Function to request SMS permission
Future<void> _requestSmsPermission() async {
if (await Permission.sms.request().isGranted) {
// Permission is granted, query SMS messages
_querySmsMessages();
}
}
// Function to query SMS messages
Future<void> _querySmsMessages() async {
final messages = await _query.querySms(
kinds: [
SmsQueryKind.inbox,
SmsQueryKind.sent,
],
count: 10,
);
debugPrint('sms inbox messages: ${messages.length}');
setState(() => _messages = messages);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter SMS Inbox App',
theme: ThemeData(
primarySwatch: Colors.teal,
),
home: Scaffold(
appBar: AppBar(
title: const Text('SMS Inbox Example'),
),
body: Container(
padding: const EdgeInsets.all(10.0),
child: _messages.isNotEmpty
? _MessagesListView(
messages: _messages,
)
: Center(
child: Text(
'No messages to show.\n Tap refresh button...',
style: Theme.of(context).textTheme.headline6,
textAlign: TextAlign.center,
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _querySmsMessages,
child: const Icon(Icons.refresh),
),
),
);
}
}
class _MessagesListView extends StatelessWidget {
const _MessagesListView({
Key? key,
required this.messages,
}) : super(key: key);
final List<SmsMessage> messages;
@override
Widget build(BuildContext context) {
return ListView.builder(
shrinkWrap: true,
itemCount: messages.length,
itemBuilder: (BuildContext context, int i) {
var message = messages[i];
return ListTile(
title: Text('${message.sender} [${message.date}]'),
subtitle: Text('${message.body}'),
);
},
);
}
}