Daibaku
Daibaku

Reputation: 12566

flutter how can I get title out of this in Dart?

I'm struggling with getting "title" which is "You have a new follower" out of this.

var message = {aps: {badge: 1, alert: {title: You have a new follower}, category: FLUTTER_NOTIFICATION_CLICK, sound: default}, google.c.a.e: 1, gcm.message_id: 0:1540775699450397%df3f58d1dgsdfig1}

I tried message['aps'], message['title'] and message['alert'] but everything return null. How can I do this correctly?

Upvotes: 0

Views: 258

Answers (1)

diegoveloper
diegoveloper

Reputation: 103411

If your JSON is well formatted , this should work:

   String yourTitle = message['aps']['alert']['title'];

Full code:

 import 'dart:convert';
 ...

        String message = '{ "aps": { "badge": 1, "alert": { "title": "You have a new follower" }, "category": "FLUTTER_NOTIFICATION_CLICK", "sound": "default" }, "google.c.a.e": 1, "gcm.message_idt": "0:1540775699450397%df3f58d1dgsdfig1" }';
        final parsed = json.decode(message);
        final title = parsed['aps']['alert']['title'];
        print("title: $title");

Upvotes: 3

Related Questions