GrandMagus
GrandMagus

Reputation: 742

Mapping out values from data Flutter

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?

Upvotes: 3

Views: 395

Answers (2)

GrandMagus
GrandMagus

Reputation: 742

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!

Upvotes: 0

sanjay
sanjay

Reputation: 636

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

Upvotes: 2

Related Questions