anoop4real / Flutter_MQTT

An attempt with Flutter
59 stars 33 forks source link

Requesting Guidance: Why is this project not using streams? #2

Open Tejas-MD opened 4 years ago

Tejas-MD commented 4 years ago

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

anoop4real commented 4 years ago

I am using Provider and Notifier Object for the purpose, I felt it more convenient to handle than using streams... In the StreamBuilder are you getting any values....

Tejas-MD commented 4 years ago

In the StreamBuilder are you getting any values....

Hi, Thanks for the reply!

To check the same, I checked hasData for snapshot and it resulted in false as expected in the beginning, during the initial build.

But, the widget didn't seem to rebuild upon successful subscription. The problem is, I'm unsure of which of the two might be the problem:

  1. The Stream is not transmitting any data due to improper application of the same.

  2. The widget inside hasData==true is not rebuilding because there is no setState . (I couldn't add a setState anywhere nearby despite efforts.)

Please help diagnose and find solutions. I'm stuck.