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?
Asked
Active
Viewed 353 times
3

GrandMagus
- 600
- 3
- 12
- 37
2 Answers
2
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']);
}

sanjay
- 626
- 1
- 7
- 12
-
you can also create a message class and the add a fromJson factory – Golden Lion Jan 10 '22 at 15:48
-
I had to make some small changes for it to work, thanks a lot for the pointers! – GrandMagus Jan 10 '22 at 16:31
0
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!

GrandMagus
- 600
- 3
- 12
- 37