Reputation: 43
i'm trying to print an SMS, but i'm just printing "instance of..."
this is an app which show all sms in the smartphone on the display
i'm using the sms plugin
class _HomeState extends State<Home> {
Future<List> getSMS() async {
SmsQuery query = new SmsQuery();
List<SmsMessage> messages = await query.getAllSms;
return messages;
}
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: FutureBuilder<List>(
future: getSMS(),
builder: (context, snapshot) {
switch(snapshot.connectionState){
case ConnectionState.none:
case ConnectionState.waiting:
default:
if(snapshot.hasError){
return new Center(
child: Text("Erro ao Carregar Dados :(",
style: TextStyle(
color: Colors.black,
fontSize: 25.0),
textAlign: TextAlign.center,)
);
} else {
debugPrint(snapshot.data[0].toString());
}
}
return new Center(
child: Text(snapshot.data[0].toString(),
style: TextStyle(
color: Colors.black,
fontSize: 25.0),
textAlign: TextAlign.center,)
);
},
),
);
}
}
all i need is to print the sms on display, but i don't know how to print an instance of object in dart
Upvotes: 4
Views: 24794
Reputation: 10953
If you want to print in console all the messages:
print(snapshot.data.map((message) => message.body));
If you want to print in console the first message:
print(snapshot.data.first.body);
If you want to show in the Text
widget all the messages:
Text('${snapshot.data.map((message) => message.body)}')
If you want to show in the Text
widget the first message:
Text('${snapshot.data.first.body}')
If you want to display a list of Text
widgets with all the messages:
Column(
children: snapshot.data.map((message) => Text('${message.body}')).toList(),
)
Upvotes: 1
Reputation: 2304
You can only print Strings (since that's what the print console does). If a class doesn't have a toString()
method built in, then you'll need to figure out where a string is. On that package it looks like SmsMessage
has a body
parameter which is a string. Since you have a list of SmsMessages you'll need to iterate through them in something like a forLoop and print their subsequent body
parameters. To print the first message you can do print(messages.first.body)
Upvotes: 2