【问题标题】:Problem with decoding JSON data from Pusher API in flutter在flutter中从Pusher API解码JSON数据的问题
【发布时间】:2021-11-20 11:52:14
【问题描述】:

我使用 Pusher API 作为实时数据库。 我可以成功连接并获取 JSON 格式的数据。 问题:我无法解码。我正在使用 jsonDecode();但这不起作用。 Here is the screenshot of my code.

有人有解决办法吗?有没有其他解码方法?

Future<void> _initPusher() async{
  PusherClient pusher;
  Channel channel;
  pusher = new PusherClient(
    "adb844af65547f4a67cf",
    PusherOptions(
      cluster: "mt1",
    ),
    enableLogging: true,
  );

  channel = pusher.subscribe("my-channel-chat");

  pusher.onConnectionError((error) {
    print("error: ${error!.message}");
  });

  channel.bind('my-event-chat', (event) {
    // print(event!.data.toString());
    final data = jsonDecode(event!.data.toString());
    print (data);
  });

}

【问题讨论】:

    标签: flutter api crash decode pusher


    【解决方案1】:

    您需要在行尾添加一个逗号以使其成为有效的 JSON。目前是:

    {
    "sender":"you"
    "message":"Something to say"
    "time":"00.00"
    }
    

    试试

    {
    "sender":"you",
    "message":"Something to say",
    "time":"00.00"
    }
    

    您可以使用https://jsonlint.com/等服务来检查您的JSON是否有效。

    【讨论】:

      猜你喜欢
      • 2019-04-14
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 2020-12-31
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多