如何在 flutter 中将数据作为 JSON 发送到 MQTT 代理
How to Send data as JSON over to MQTT broker in flutter
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电机”,
“积累”:70,
"F/R": "转发",
“中断”:“假”,
“键”:“真”
}
帮助我在按下按钮时将单个 json 数据传递给 mqtt 客户端
在发布方法中传递值并在那里进行更改它会起作用
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!);}
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电机”, “积累”:70, "F/R": "转发", “中断”:“假”, “键”:“真” }
帮助我在按下按钮时将单个 json 数据传递给 mqtt 客户端
在发布方法中传递值并在那里进行更改它会起作用
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!);}