我如何在Dart中获得标题呢?

时间:2018-10-29 01:22:43

标签: dart flutter

我正在努力从中获得“标题”,即“您有一个新的关注者”。

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}

我尝试了message['aps']message['title']message['alert'],但是一切都返回null。如何正确执行此操作?

1 个答案:

答案 0 :(得分:3)

如果您的JSON格式正确,则应该可以:

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

完整代码:

 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");