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?

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

you can also create a message class and the add a fromJson factory
I had to make some small changes for it to work, thanks a lot for the pointers!
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!

1 Comment

Great to know that you solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.