问题
I am new to Flutter (coming from Android background) and want to implement MQTT client in flutter.
This is what i want:
MQTT client which should be "alive" when the app is open/not killed by OS.
I don't want to run it in background (without the app open, i know there is issue doing it in iOS, read it somewhere) but while the app is open.
Updating UI/State based on messages coming in subscribed topics.
I looked into pub and found 2 packages, mqtt and mqtt_client, they look like non-Flutter implementation of MQTT in dart.
I have an idea how to use one of this lib to make it work with flutter but not sure if it will work:
Create and implement MQTTObserver sateful widget which will async-ly connect to broker and listen for messages to particular topic and update the state accordingly.
Will wrap this widget around MaterialApp
Any widget down the tree would be able to access the state of MQTTObserver widget to reflect it in their UI.
Do you think it is a good idea? will it work? do you have better idea? or should i implement platform-specific implementation for MQTT (should be my last option).
回答1:
The mqtt_client package is already being used in flutter although under development, I don't use flutter myself but several flutter users have raised issues on it, see [here][1]
[1]: https://github.com/shamblett/mqtt_client/issues/16 for an example, there others in the issues list, maybe you could ask one of these flutter devs your question.
来源:https://stackoverflow.com/questions/51836505/implementing-mqtt-in-flutter