I'm having trouble extracting the values of the data received from the push notification:
Message data: {default: {"event":"text","id":"23Vlj0BwSA7xKtsf4IbFCHAIsNr"}}, where I tried a lot of .map methods etc. but I always get null values. Is there an easy approach to get the data from the message.data, so I can extract the value of the event key and the id value from the id key?
Add a comment
|
2 Answers
Can you try with the below code?
import 'dart:convert';
const rawJson =
'{"default": {"event":"text","id":"23Vlj0BwSA7xKtsf4IbFCHAIsNr"}}';
void parse() {
final value = json.decode(rawJson);
print(value['default']['event']);
print(value['default']['id']);
}
2 Comments
ListenSoftware Louise Ai Agent
you can also create a message class and the add a fromJson factory
GrandMagus
I had to make some small changes for it to work, thanks a lot for the pointers!
Thanks to @sanjay for the help, his solution wasn't working for me but it was close enough, only these 2 small changes worked:
NOTICE: Apart from @sanjay's answer I had to change json.decode to jsonDecode and I had to put a variable var instead of the constant value. I can understand the var and const, but I'm not sure about the jsonDecode method why I had to change it but it worked like this.
var value = jsonDecode(message.data['default']);
var event = value['event'];
var id = value['id'];
print(id);
Output:
23Vlj0BwSA7xKtsf4IbFCHAIsNr
Thanks for the help!
1 Comment
sanjay
Great to know that you solved the issue.
