如何在 flutter 中将数据以 JSON 形式发送到 MQTT 代理

发布于 2025-01-09 14:31:38 字数 752 浏览 0 评论 0原文

    return RaisedButton(
      color: Colors.green,
      disabledColor: Colors.grey,
      textColor: Colors.white,
      disabledTextColor:Colors.black38 ,
      child: Text("Break"),
      onPressed: state == MQTTAppConnectionState.connectedSubscribed
          ? () {
        setState(() {
          value = 0;
          action = jsonData;
        });
        publishMessage(action);
      }
          : null, //
    );
  }

I have multiple buttons in my app that passes different message  to mqtt client


I want to pass  different json data to mqtt client for each button 


sample json data for single button 

{ "电机":"BLDC电机", “ACC”:70, “F/R”:“前进”, "Break":"假", “关键”:“真实” 端

按下按钮时帮助我将单个 json 数据传递给 mqtt 客户

    return RaisedButton(
      color: Colors.green,
      disabledColor: Colors.grey,
      textColor: Colors.white,
      disabledTextColor:Colors.black38 ,
      child: Text("Break"),
      onPressed: state == MQTTAppConnectionState.connectedSubscribed
          ? () {
        setState(() {
          value = 0;
          action = jsonData;
        });
        publishMessage(action);
      }
          : null, //
    );
  }

I have multiple buttons in my app that passes different message  to mqtt client


I want to pass  different json data to mqtt client for each button 


sample json data for single button 

{
"Motor":"BLDC motor",
"Acc": 70,
"F/R": "Forward",
"Break":"False",
"Key":"True"
}

help me to pass single json data to mqtt client when the button is pressed

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

各空 2025-01-16 14:31:39

在发布方法中传递值并进行更改,它将起作用

return RaisedButton(
  color: Colors.green,
  disabledColor: Colors.grey,
  textColor: Colors.white,
  disabledTextColor:Colors.black38 ,
  child: Text("Break"),
  onPressed: state == MQTTAppConnectionState.connectedSubscribed
      ? () {
    setState(() {
      break1 = "True";
      value = 0;
    });
    publish(break1);
  }
      : null,//
);

void publish(String break1) {
final MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
builder.addString(
  json.encode(
    {
      "break": "$break1",
    }
  )
);
_client!.publishMessage(_topic, MqttQos.exactlyOnce, builder.payload!);}

Pass the values in the publish method and make this changes there it will work

return RaisedButton(
  color: Colors.green,
  disabledColor: Colors.grey,
  textColor: Colors.white,
  disabledTextColor:Colors.black38 ,
  child: Text("Break"),
  onPressed: state == MQTTAppConnectionState.connectedSubscribed
      ? () {
    setState(() {
      break1 = "True";
      value = 0;
    });
    publish(break1);
  }
      : null,//
);

void publish(String break1) {
final MqttClientPayloadBuilder builder = MqttClientPayloadBuilder();
builder.addString(
  json.encode(
    {
      "break": "$break1",
    }
  )
);
_client!.publishMessage(_topic, MqttQos.exactlyOnce, builder.payload!);}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文