shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
548 stars 176 forks source link

The getter 'connectionStatus' was called on null. #180

Closed larteyjoshua closed 4 years ago

larteyjoshua commented 4 years ago

I wrote this flutter MQTT code which I named it mqtt_page. dart. It connected well anytime I called the initializemqtt() function at main.dart while running the app and allows for my sensor data transfers using a flutter stream controller. I again wrote a function called automatic() which I am expecting to publish a message on the broker anytime I called it with a raisedButton. But it is throwing this error. What are mine not doing right?

'class Mqttwrapper { StreamController<Map<String, dynamic>> mqttController =StreamController.broadcast(); static final Mqttwrapper instance = Mqttwrapper._internal(); factory Mqttwrapper () => instance; Mqttwrapper._internal();

DatabaseHelper databaseHelper = DatabaseHelper(); /// An annotated simple subscribe/publish usage example for mqtt_client. Please read in with reference /// to the MQTT specification. The example is runnable, also refer to test/mqtt_client_broker_test...dart /// files for separate subscribe/publish tests. MqttClient client; String clientIdentifier = 'android'; Future initializemqtt() async { final MqttClient client = MqttClient("mqtt.dioty.co", ""); client.port = 1883; client.logging(on: false); client.keepAlivePeriod = 30; client.onConnected = _onConnect; client.onDisconnected = _onDisconnect; client.onSubscribed = onSubscribed; final MqttConnectMessage connMess = new MqttConnectMessage() .withClientIdentifier(clientIdentifier) .startClean() .keepAliveFor(60); print("EXAMPLE::MQTT client connecting...."); client.connectionMessage = connMess; try { await client.connect("xxxxxxxxx@gmail.com", "xxxxxxx"); } catch (e) { print("EXAMPLE::client exception - $e"); client.disconnect(); } /// Check we are connected if (client.connectionStatus.state == MqttConnectionState.connected) { print("EXAMPLE::MQTT client connected"); } else { print( "EXAMPLE::ERROR Mosquitto client connection failed - disconnecting, state is ${client .connectionStatus.state}"); client.disconnect(); exit(-1); } /// Ok, lets try a subscription final String topic = "/xxxxxx@gmail.com/test"; // Not a wildcard topic client.subscribe(topic, MqttQos.atMostOnce); final String topictwo = "/xxxxx@gmail.com/SensorData"; // Not a wildcard topic client.subscribe(topictwo, MqttQos.atMostOnce); /// The client has a change notifier object(see the Observable class) which we then listen to to get /// notifications of published updates to each subscribed topic. client.updates.listen((List c) { final MqttPublishMessage recMess = c[0].payload as MqttPublishMessage; final String pt = MqttPublishPayload.bytesToStringAsString(recMess.payload.message); print( "EXAMPLE::Change notification:: topic is <${c[0] .topic}>, payload is <-- ${pt} -->"); print(""); final String topicSensor = "/xxxxx@gmail.com/SensorData"; if (("${c[0].topic}") == (topicSensor)) { final datasensor = json.decode(pt); mqttController.add(datasensor); } }); print("EXAMPLE::Publishing our topic"); final String pubTopic = "/xxxxx@gmail.com/test"; final MqttClientPayloadBuilder builder = new MqttClientPayloadBuilder(); builder.addString("Hello from mqtt_client"); client.publishMessage(pubTopic, MqttQos.atMostOnce, builder.payload); client.subscribe(pubTopic, MqttQos.atMostOnce); } void automatic (){ if (client.connectionStatus.state == MqttConnectionState.connected) { print("EXAMPLE::Publishing our Automatic"); final String autoPush = "/xxxx@gmail.com/test"; final builder = MqttClientPayloadBuilder(); builder.addString("automatic"); client.publishMessage(autoPush, MqttQos.atLeastOnce, builder.payload); client.subscribe(autoPush, MqttQos.atMostOnce); } } /// The subscribed callback void onSubscribed(String topic) { print("EXAMPLE::Subscription confirmed for topic $topic"); } /// The unsolicited disconnect callback void onDisconnected() { print("EXAMPLE::OnDisconnected client callback - Client disconnection"); exit(-1); } _onConnect() { print("mqtt connected"); } _onDisconnect() { print("mqtt disconnected"); } } }' child: RaisedButton( color: Colors.blue, child: Text('Automatic'), onPressed: () { Mqttwrapper().automatic();}, ),

I/flutter (23487): EXAMPLE::MQTT client connecting.... I/flutter (23487): mqtt connected I/flutter (23487): EXAMPLE::MQTT client connected I/flutter (23487): EXAMPLE::Publishing our topic I/flutter (23487): EXAMPLE::Subscription confirmed for topic /xxxxx@gmail.com/test I/flutter (23487): EXAMPLE::Subscription confirmed for topic /xxxxx@gmail.com/SensorData I/flutter (23487): EXAMPLE::Change notification:: topic is </xxxxx@gmail.com/test>, payload is <-- Hello from mqtt_client --> I/flutter (23487): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════ I/flutter (23487): The following NoSuchMethodError was thrown while handling a gesture: I/flutter (23487): The getter 'connectionStatus' was called on null. I/flutter (23487): Receiver: null I/flutter (23487): Tried calling: connectionStatus I/flutter (23487): When the exception was thrown, this was the stack: I/flutter (23487): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5) I/flutter (23487): #1 Mqttwrapper.automatic (package:iot_irrigation_app/mqtt_page.dart:101:16) I/flutter (23487): #2 _MyHomePageState.build.<anonymous closure> (package:iot_irrigation_app/pumpcontrol.dart:65:57) I/flutter (23487): #3 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:706:14) I/flutter (23487): #4 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:789:36) I/flutter (23487): #5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24) I/flutter (23487): #6 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:486:11) I/flutter (23487): #7 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:264:5) I/flutter (23487): #8 BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:199:7) I/flutter (23487): #9 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:467:9) I/flutter (23487): #10 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:76:12) I/flutter (23487): #11 PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:117:9) I/flutter (23487): #12 _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8) I/flutter (23487): #13 PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:115:18) I/flutter (23487): #14 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:7) I/flutter (23487): #15 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19) I/flutter (23487): #16 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22) I/flutter (23487): #17 GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7) I/flutter (23487): #18 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7) I/flutter (23487): #19 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7) I/flutter (23487): #23 _invoke1 (dart:ui/hooks.dart:273:10) I/flutter (23487): #24 _dispatchPointerDataPacket (dart:ui/hooks.dart:182:5) I/flutter (23487): (elided 3 frames from package dart:async) I/flutter (23487): Handler: "onTap" I/flutter (23487): Recognizer: I/flutter (23487): TapGestureRecognizer#0fb7c I/flutter (23487): ════════════════════════════════════════════════════════════════════════════════════════════════════

shamblett commented 4 years ago

I don't develop in flutter, however the stack trace seems to suggest that in the automatic() method this line

if (client.connectionStatus.state == MqttConnectionState.connected) 

doesn't have a valid client, i.e. the client is null, I don't know why this would occur, however this doesn't seem to suggest there is anything wrong with the client itself, what bug are you reporting exactly?