'MQTT for flutter web fails to connect

Below is my code for connecting HIVE broker from Flutter chrome web. In advance, I would like to note that it works for windows app with no issues. However, with browser, i get below error

broker.mqttdashboard.com
    at Object.throw_ [as throw] (http://localhost:54330/dart_sdk.js:5069:11)
    at mqttservice.MQQTTService.new.connectClientToBroker (http://localhost:54330/packages/baycontrolwindows/services/mqttservice.dart.lib.js:44:23)
    at connectClientToBroker.throw (<anonymous>)
    at http://localhost:54330/dart_sdk.js:40632:38
    at _RootZone.runBinary (http://localhost:54330/dart_sdk.js:40501:59)
    at _FutureListener.thenAwait.handleError (http://localhost:54330/dart_sdk.js:35435:33)
    at handleError (http://localhost:54330/dart_sdk.js:36001:51)
    at Function._propagateToListeners (http://localhost:54330/dart_sdk.js:36027:17)
    at _Future.new.[_completeError] (http://localhost:54330/dart_sdk.js:35864:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:54330/dart_sdk.js:35913:31)
    at Object._microtaskLoop (http://localhost:54330/dart_sdk.js:40764:13)
    at _startMicrotaskLoop (http://localhost:54330/dart_sdk.js:40770:13)
    at http://localhost:54330/dart_sdk.js:36247:9```
My code is as below:
class MQQTTService {
  MqttBrowserClient client = MqttBrowserClient.withPort(
      'broker.mqttdashboard.com', 'flutterListener', 8000,
      maxConnectionAttempts: 1);

  void onConnectedClient() {
    print("Client has been succesffully connected");
  }

  void onSubscribedToTopic(String topicName) {
    print("Client subscribed to the topic successfully:" + topicName);
  }

  void onSubscribedToTopicFailed(String topicName) {
    print("Client can not subscribe to the topic:" + topicName);
  }


  Future<void> connectClientToBroker() async {
    client.onConnected = onConnectedClient;

    try {
      await client.connect();
    } on Exception catch (e) {
      throw ("Error happened:" + e.toString());
    }
  }
can someone please give me a hint where do I get things wrong?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source