Hi,
I'm working on a similar MQTT based flutter project and I had to use streams for rebuilding my widgets upon message arrival. Why is it not essential here?
My Stream is not working tho, I'm using a stream controller to sink payload right after the the variable 'pt' is created and assigned a value. I'm listening to it with StreamBuilder Widget on main.dart and rebuilding a card based on it.
The issue is: the card does not rebuild upon message arrival. I can see the message print on the console(using Print) but the listener/subsriber doesn't seem to be working well.
Code:
class PayloadGiver {
final mqttStream = StreamController<String>();
void payloadGiver(String subscribeTopic) {
if (mqttInitComplete == true) {
print('Subscribing to $subscribeTopic');
// Not a wildcard topic
// client.subscribe(subscribeTopic2, MqttQos.atMostOnce);
client.subscribe(subscribeTopic, MqttQos.atMostOnce);
client.updates.listen((List<MqttReceivedMessage<MqttMessage>> c) {
final MqttPublishMessage recMess = c[0].payload;
final pt =
MqttPublishPayload.bytesToStringAsString(recMess.payload.message);
print('The payload is : $pt');
mqttStream.sink.add(pt);
});
}
}
Listener:
StreamBuilder<String>(
stream: mqttStream,
builder: (context, snapshot) {
// if (!snapshot.hasData) return Text('No Data');
// if (snapshot.connectionState == ConnectionState.waiting)
// print("Still Waiting for Data");
// print('Currently, I have: ' + snapshot.data);
if (snapshot.data == 'true')
return SwitchCardOn();
else if (snapshot.data == 'false') {
print(
"I recieved false but did not build the widget");
return SwitchCardOff();
} else
return Text('Waiting');
}),
Please help
Hi,
I'm working on a similar MQTT based flutter project and I had to use streams for rebuilding my widgets upon message arrival. Why is it not essential here?
My Stream is not working tho, I'm using a stream controller to sink payload right after the the variable 'pt' is created and assigned a value. I'm listening to it with StreamBuilder Widget on main.dart and rebuilding a card based on it.
The issue is: the card does not rebuild upon message arrival. I can see the message print on the console(using Print) but the listener/subsriber doesn't seem to be working well.
Code:
Listener:
Please help