shamblett / mqtt_client

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

Null check operator on auto reconnect #437

Closed mis-kcn closed 1 year ago

mis-kcn commented 1 year ago

Code is failing at mqtt_client.dart Line 353 at clientEventBus!.

Error occurs at v.9.7.2, no clear MRE at the moment.

void doAutoReconnect({bool force = false}) {
    if (!autoReconnect) {
      MqttLogger.log(
          'MqttClient::doAutoReconnect - auto reconnect is not set, exiting');
      return;
    }

    if (connectionStatus!.state != MqttConnectionState.connected || force) {
      // Fire a manual auto reconnect request.
      final wasConnected =
          connectionStatus!.state == MqttConnectionState.connected;
      clientEventBus!
          .fire(AutoReconnect(userRequested: true, wasConnected: wasConnected));
    }
  }
shamblett commented 1 year ago

Ok the lfile mosquito_bloc.dart isnt from this package so Im not sure what this is, you dont say what the failure is, Im guessing a null exception, on the event bus this is usually caused by the client being disconnected and closed.

Ill need a log to look any further.

mis-kcn commented 1 year ago

Oops sorry about that, it wasn't with the mosquitto_bloc that was my own bloc implementation of the whole thing.

Null check operator used on a null value occurs at mqtt_client.dart at the doAutoReconnect function, at the clientEventBus! line.

jehhxuxu commented 1 year ago

Any news on this? I have the problem as well... log from firebase

Fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError
Bad state: Cannot add new events after calling close. Error thrown null.
_BroadcastStreamController.add (_BroadcastStreamController.java)
EventBus.fire (event_bus.dart:61)
MqttConnectionHandlerBase.autoReconnect (mqtt_client_mqtt_connection_handler_base.dart:155)
shamblett commented 1 year ago

You've closed(disconnected) the client somehow before autoreconnect starts, do you have a full log of what happened before this point?

jehhxuxu commented 1 year ago

Nope, crashlytics show just this piece of error... We are sending our implementation of connect and the _onAutoReconnect function just verifies if our token is not null and do connect again... if you are available I can show the code...

Screenshot 2023-03-10 at 19 02 58 Screenshot 2023-03-10 at 19 05 02

shamblett commented 1 year ago

Whatever you are doing you have closed the event bus, hence the bad state, this being picked up in auto reconnect, its not something it has done, its something it has found. Do you have an onDisconnected callback?

jehhxuxu commented 1 year ago

Yes, we have an onDisconnected callback, how can I simulate/force the autoReconnect?

shamblett commented 1 year ago

You need to disconnect the broker, so kill the network connection, or the broker, or switch from GSM to wireless etc.