Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LateInitializationError: Field 'messageStream' has not been initialized. #16

Merged
merged 4 commits into from
Oct 10, 2021

Conversation

stijn1989
Copy link
Contributor

When you have a Dart program which starts and connects to a MQTT broker. But the broker is offline, so you start the autoConnect proces.

try {
  await mqtt.connect();
  mqtt.sub();
} catch (e) {
  mqtt.client.doAutoReconnect();
}

When the MQTT broker is back online, it connects. But an exception is thrown because the messageStream isn't initialized. The late property is only set in the connect() method and should also be set in the connectAuto().

The mqtt_browser_ws_connection.dart is correct. The mqtt_server_*_connection.dart not.

Also the event handler onAutoReconnected isn't set and by this never called.

client.onAutoReconnected = () {
  if(client.subscriptionsManager!.subscriptions.isEmpty) {
    //initialize the subscriptions
  }
};

The onAutoReconnected event handler was never set tot the connection handler.
When connecting fails on first call of connect() on the MQTT client. Then you invoke the doAutoReconnect() method (and also set the autoReconnect property to true). When it connects, the server connections don't have a readWrapper and messagStream, which result in exceptions.
@shamblett
Copy link
Owner

I'm not sure why you need to recreate readWrapper and messageStream in the connectAuto method, these are created in the connect method which you must call first and as far as I can see never subsequently destroyed. Your use case seems a little odd, you can't invoke autoConnect until connect has worked. Another way to do this is to move the setting of readWrapper and messageStream to the top of the connect method, i.e. they are in place before we start the socket connect, not after so they should persist from then on.

@stijn1989
Copy link
Contributor Author

The objects are created when the socket has a connection. When there is no successfull connection, these objects aren't created. Moving them up, is a perfect solution. Why not make those fields final and initialised?

I invoke the doAutoConnect when the connect() fails. So the code is trying to attempt to connect. The event handlers are used to give information back to the logs.

I'm creating a program that needs the MQTT broker to work. And it should solve it's connection without user interaction.

@shamblett
Copy link
Owner

I see what you are saying, yes we could make them final and initialised. OK i understand what you are trying to do now.
Do you want to do the update or do you want me to do it?

@stijn1989
Copy link
Contributor Author

I can do it tonight. I revert the commit and make those fields final and remove them in the connect and autoConnect methods. ok?

@shamblett
Copy link
Owner

Yep, sounds good to me, thanks.

Initialize the readWrapper and messageStream in the base connection class.

Removed the creation of those objects in the connection classes in the socket callbacks.
@shamblett shamblett merged commit 6ca8386 into shamblett:master Oct 10, 2021
@shamblett
Copy link
Owner

Package re published now at version 3.1.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants