shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

Error when running with flutter #22

Closed vinicentus closed 6 years ago

vinicentus commented 6 years ago

So I get this error when trying to run the example as a flutter (0.5.1) app. Any ideas on what could be wrong?

I/flutter (18829): EXAMPLE::Mosquitto client connecting.... E/flutter (18829): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception: E/flutter (18829): NoSuchMethodError: The setter 'disconnectRequested=' was called on null. E/flutter (18829): Receiver: null E/flutter (18829): Tried calling: disconnectRequested=true E/flutter (18829): #0 Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:46:5) E/flutter (18829): #1 SynchronousMqttConnectionHandler._performConnectionDisconnect (file:///C:/Program%20Files%20(other)/flutter/.pub-cache/hosted/pub.dartlang.org/mqtt_client-1.9.0/lib/src/connectionhandling/mqtt_client_synchronous_mqtt_connection_handler.dart:83:16) E/flutter (18829): #2 SynchronousMqttConnectionHandler.disconnect (file:///C:/Program%20Files%20(other)/flutter/.pub-cache/hosted/pub.dartlang.org/mqtt_client-1.9.0/lib/src/connectionhandling/mqtt_client_synchronous_mqtt_connection_handler.dart:76:5) E/flutter (18829): #3 MqttClient.disconnect (file:///C:/Program%20Files%20(other)/flutter/.pub-cache/hosted/pub.dartlang.org/mqtt_client-1.9.0/lib/src/mqtt_client.dart:182:25) E/flutter (18829): #4 main (file:///C:/Users/vince/AndroidStudioProjects/mqtt_test/lib/main.dart:63:12) E/flutter (18829): E/flutter (18829): #5 _startIsolate. (dart:isolate/runtime/libisolate_patch.dart:279:19) E/flutter (18829): #6 _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)

shamblett commented 6 years ago

OK, I think I've seen this before, your disconnecting from the broker, try it again with logging on,

client.logging(true);

shamblett commented 6 years ago

Bit more on this, I've fixed the exception, it actually means the broker has not accepted the connection request for on of the following

if (ackMsg.variableHeader.returnCode == MqttConnectReturnCode.brokerUnavailable || ackMsg.variableHeader.returnCode == MqttConnectReturnCode.identifierRejected || ackMsg.variableHeader.returnCode == MqttConnectReturnCode.unacceptedProtocolVersion || ackMsg.variableHeader.returnCode == MqttConnectReturnCode.notAuthorized || ackMsg.variableHeader.returnCode == MqttConnectReturnCode.badUsernameOrPassword) {

or the incoming ack message cannot be decoded at all.

vinicentus commented 6 years ago

Thank you, I will try the new version soon...

shamblett commented 6 years ago

Republished version 1.9.1

vinicentus commented 6 years ago

Maybe the names should be changed?

error: The name 'ConnectionState' is defined in the libraries 'async.dart' and 'mqtt_client.dart'. (ambiguous_import at [mqtt_test2] lib\main.dart:81)
error: The name 'ChangeNotifier' is defined in the libraries 'change_notifier.dart' and 'change_notifier.dart'. (ambiguous_import at [mqtt_test2] lib\main.dart:93)
error: The name 'ChangeNotifier' is defined in the libraries 'change_notifier.dart' and 'change_notifier.dart'. (ambiguous_import at [mqtt_test2] lib\main.dart:101)

One ChangeNotifier is in the flutter package, and the other is in observable-0.22.1+3, both of which are dart packages.

vinicentus commented 6 years ago
I/flutter (21503): EXAMPLE::Mosquitto client connecting....
I/flutter (21503): 2018-07-12 15:56:10.228887 -- SynchronousMqttConnectionHandler::disconnect
I/flutter (21503): 2018-07-12 15:56:10.245624 -- MqttConnectionHandler::sendMessage - MQTTMessage of type MqttMessageType.disconnect
I/flutter (21503): Header: MessageType = MqttMessageType.disconnect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (21503): 2018-07-12 15:56:10.246130 -- MqttConnectionHandler::sendMessage - not connected
I/flutter (21503): EXAMPLE::ERROR Mosquitto client connection failed - disconnecting, state is ConnectionState2.disconnected
Lost connection to device.
Error writing "build/flutter_assets/AssetManifest.json" to DevFS: HttpException: Connection closed before full header was received, uri = http://127.0.0.1:8106/
Error writing "build/flutter_assets/FontManifest.json" to DevFS: HttpException: Connection closed before full header was received, uri = http://127.0.0.1:8106/

Flutter doesn't really play nice with this. It looses connection to my test device. It could be my fault but idk.

shamblett commented 6 years ago

Try prefixing your import of the mqtt_client,

import 'package:mqtt_client/mqtt_client.dart' as mqtt;

then anywhere where you use ant of it prefix it with 'mqtt.' this should stop name clashes. You can do this with any packages.

shamblett commented 6 years ago

Yes, I never used flutter but this looks wrong, 'uri = http://127.0.0.1:8106/', mqtt_client just connects to its broker over a socket as shown in the example e.g test.mosquito.org on port 1883(default), there's no HTTP involved, also its logging as 127.0.0.1 which is localhost.

I'm sure there are other flutter users that are using the client, maybe ask on flutter list.

vinicentus commented 6 years ago

I'm not really sure what the problem is here, but I got it all working with this fork and the example (located in readme.md). https://github.com/benbehringer/mqtt_client

If you want to dig further I'd be happy to help although my experience with flutter and dart is pretty minimal. Otherwise you can close this issue.

shamblett commented 6 years ago

Ok, thanks

cnqiuxue commented 5 years ago

Built build/app/outputs/apk/debug/app-debug.apk. Installing build/app/outputs/apk/app.apk... Syncing files to device YF XXXG... I/Process (19514): Sending signal. PID: 19514 SIG: 9 Lost connection to device. Error writing "build/flutter_assets/AssetManifest.json" to DevFS: HttpException: , uri = http://127.0.0.1:49865/ Error writing "build/flutter_assets/FontManifest.json" to DevFS: HttpException: , uri = http://127.0.0.1:49865/