如何使用推送器发送数据类型映射?

How to send data type map flutter with pusher?

我想发送对象数据,如示例Pusher 1.0.0,要发送的数据是文件夹的形式,并遵循我的编码:

import 'package:pusher/pusher.dart';

Future<void> mmain(Map data) async {
  Pusher pu = new Pusher('PUSHER_APP_ID', 'PUSHER_APP_KEY',
      'PUSHER_APP_SECRET', PusherOptions(cluster: 'ap1'));
  // Map data = {'message': 'Hello world'};
  Response res = await pu.trigger(['channelTa'], 'tombol', data);
  print(res);
}

我用开关打电话:

child: Switch(
       value: widget.mdata[0]['m' + index.toString()],
       onChanged: (value) {
            setState(() {
               widget.mdata[0]['m' + index.toString()] =value;
               widget.mdata.asMap().forEach((index, value) {
                         String aja = "m$index";
                         tbl.update(aja, (value) => value);
               });
               mmain(tbl);
            });
       }),

我的编码出了什么问题,所以它显示以下错误:

Reload already in progress, ignoring request
Restarted application in 4.457ms.
D/PusherPlugin(15412): onCancel args: null
I/flutter (15412): CONNECTING
I/flutter (15412): CONNECTED
E/flutter (15412): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FormatException: Unexpected character (at character 2)
E/flutter (15412): {m0: false, m1: false, m2: false, m3: false, m4: false, m5: false}
E/flutter (15412):  ^
E/flutter (15412):
[38;5;244mE/flutter (15412): #0      _ChunkedJsonParser.fail  (dart:convert-patch/convert_patch.dart:1394:5)[39;49m
[38;5;244mE/flutter (15412): #1      _ChunkedJsonParser.parse  (dart:convert-patch/convert_patch.dart:924:48)[39;49m
[38;5;244mE/flutter (15412): #2      _parseJson  (dart:convert-patch/convert_patch.dart:31:10)[39;49m
[38;5;244mE/flutter (15412): #3      JsonDecoder.convert  (dart:convert/json.dart:505:36)[39;49m
[38;5;244mE/flutter (15412): #4      JsonCodec.decode  (dart:convert/json.dart:153:41)[39;49m
[38;5;248mE/flutter (15412): #5      _MyHomePageState._konekpusher.<anonymous closure>[39;49m
[38;5;248mE/flutter (15412): #6      Pusher._handleEvent[39;49m
[38;5;244mE/flutter (15412): #7      _rootRunUnary  (dart:async/zone.dart:1192:38)[39;49m
[38;5;244mE/flutter (15412): #8      _CustomZone.runUnary  (dart:async/zone.dart:1085:19)[39;49m
[38;5;244mE/flutter (15412): #9      _CustomZone.runUnaryGuarded  (dart:async/zone.dart:987:7)[39;49m
[38;5;244mE/flutter (15412): #10     _BufferingStreamSubscription._sendData  (dart:async/stream_impl.dart:339:11)[39;49m
[38;5;244mE/flutter (15412): #11     _DelayedData.perform  (dart:async/stream_impl.dart:594:14)[39;49m
[38;5;244mE/flutter (15412): #12     _StreamImplEvents.handleNext  (dart:async/stream_impl.dart:710:11)[39;49m
[38;5;244mE/flutter (15412): #13     _PendingEvents.schedule.<anonymous closure>  (dart:async/stream_impl.dart:670:7)[39;49m
[38;5;244mE/flutter (15412): #14     _rootRun  (dart:async/zone.dart:1180:38)[39;49m
[38;5;244mE/flutter (15412): #15     _CustomZone.run  (dart:async/zone.dart:1077:19)[39;49m
[38;5;244mE/flutter (15412): #16     _CustomZone.runGuarded  (dart:async/zone.dart:979:7)[39;49m
[38;5;244mE/flutter (15412): #17     _CustomZone.bindCallbackGuarded.<anonymous closure>  (dart:async/zone.dart:1019:23)[39;49m
[38;5;244mE/flutter (15412): #18     _rootRun  (dart:async/zone.dart:1184:13)[39;49m
[38;5;244mE/flutter (15412): #19     _CustomZone.run  (dart:async/zone.dart:1077:19)[39;49m
[38;5;244mE/flutter (15412): #20     _CustomZone.runGuarded  (dart:async/zone.dart:979:7)[39;49m
[38;5;244mE/flutter (15412): #21     _CustomZone.bindCallbackGuarded.<anonymous closure>  (dart:async/zone.dart:1019:23)[39;49m
[38;5;244mE/flutter (15412): #22     _microtaskLoop  (dart:async/schedule_microtask.dart:43:21)[39;49m
[38;5;244mE/flutter (15412): #23     _startMicrotaskLoop  (dart:async/schedule_microtask.dart:52:5)[39;49m
E/flutter (15412):
I/flutter (15412): {}

仅推杆 returns 空 json

问题是您的“数据”值无效 JSON。从堆栈跟踪我可以看到你正在传入:

{m0: false, m1: false, m2: false, m3: false, m4: false, m5: false}

这没有被解析为 JSON。

mN(m0 等)键需要用双引号括起来,例如

{"m0": false, "m1": false, "m2": false, "m3": false, "m4": false, "m5": false}

您可以在此处查看 - https://jsonformatter.curiousconcept.com/

在您的代码中添加如下引号:

String aja = '"m$index"';

String aja = "\"m$index\"";