shamblett / mqtt_client

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

Compiler error building Flutter for android #183

Closed aidandavis closed 4 years ago

aidandavis commented 4 years ago

Hi, I am trying to install this plugin for my flutter app, but simply having import 'package:mqtt_client/mqtt_browser_client.dart'; at the top of a class causes the following error and the app doesn't build.

Compiler message:
../flutter/.pub-cache/hosted/pub.dartlang.org/mqtt_client-7.1.0/lib/mqtt_browser_client.dart:11:8: Error: Not found: 'dart:html'
import 'dart:html';
       ^
../flutter/.pub-cache/hosted/pub.dartlang.org/mqtt_client-7.1.0/lib/src/connectionhandling/browser/mqtt_client_mqtt_browser_ws_connection.dart:52:16: Error: The method 'WebSocket' isn't defined for the class 'MqttBrowserWsConnection'.
 - 'MqttBrowserWsConnection' is from 'package:mqtt_client/mqtt_browser_client.dart' ('../flutter/.pub-cache/hosted/pub.dartlang.org/mqtt_client-7.1.0/lib/mqtt_browser_client.dart').
Try correcting the name to the name of an existing method, or defining a method named 'WebSocket'.
      client = WebSocket(uriString, protocols);
               ^^^^^^^^^
../flutter/.pub-cache/hosted/pub.dartlang.org/mqtt_client-7.1.0/lib/src/connectionhandling/browser/mqtt_client_mqtt_browser_connection.dart:30:32: Error: 'MessageEvent' isn't a type.
      client.onMessage.listen((MessageEvent e) {
                               ^^^^^^^^^^^^

Any suggestions? Looking back through the closed issues this one pops up, #144 . Doesn't have a fix for it. I am Flutter (Channel stable, v1.17.0, on Microsoft Windows [Version 10.0.18362.778], locale en-AU), and mqtt_client: ^7.1.0

shamblett commented 4 years ago

I'm not a flutter user but I'm sure flutter is a server side Dart implementation, hence it can't find dart:html in which case you will need mqtt_server_client.dart not mqtt_browser_client.dart. The mqtt_browser_client.dart is intended for use in browsers, not the Dart VM

aidandavis commented 4 years ago

Thanks for your fast response! It's working for me now. I think this advice should be added to the readme.

For Flutter, use mqtt_server_client.dart rather than mqtt_browser_client.dart.