3

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?

GrandMagus
  • 600
  • 3
  • 12
  • 37

2 Answers2

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']);
}

Output: enter image description here

sanjay
  • 626
  • 1
  • 7
  • 12
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