flutter - 解码JSON抖动

标签 flutter dart push-notification

我有这种数据格式

message": [
{
"id": 15989,
"title": "xxx",
"body": "xxx",
"type": "abc",
"data_hash": "{\"id\":\"3098\",\"number\":1}",
}, .....]

如果我这样写
print(message['data']['type']);
我可以得到abc,但是如果我写print(message['data']['data_hash']);,则会得到无效的参数错误。为什么?

我想在data_hash中获取number

这是完整的代码
 _firebaseMessaging.configure(
            onMessage: (Map<String, dynamic> message) async {
              print("===== onMessage ====");  
              try {
                print(message['data']['data_hash']);
              } catch (e) {
                print(e.toString());
              }
    });

最佳答案

data_hash行是一个json。因此,您需要对该行进行解码才能使用。

final data_hash_map = jsonDecode(message['data']['data_hash']);
print(data_hash_map); // { "id": 3098, "number": 1 }
print(data_hash_map["number"]); // for number

关于flutter - 解码JSON抖动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60648725/

相关文章:

firebase - Flutter 检查用户名是否已存在于 Firestore 数据库中

dart - 使用变量作为参数的无效常量值

flutter - http.dart onError : Invalid argument (onError): Error handler must accept one Object or one Object and a StackTrace as arguments 的无效参数

Android 应用程序不显示从 Cloud Functions 发送的弹出通知

flutter : Is provider an alternative to the BLoC pattern?

android - 在 Android Studio 中 Dart 。没有运行应用程序的设备

Flutter 应用程序不断打印 [ZeroHung]zrhung_get_config : Get config failed for wp[0x0008]

flutter - Flutter 中的可滚动导航栏

php - 规范注册 ID gcm

java - 不将值从推送通知传递给 Activity