I'm just starting to learn flutter. Currently, I'm still struggling a bit with futures.
Therefore, I'm plan to write a publish/subscribe pattern class, which should be a central message broker for async results.
Suppose, some method needs a reply from a http communication.
It would subscribe for results of the HTTP_AUTHORIZE_USER operation using my _callbackMethodCall like so:
broker.subscribe( HTTP_AUTHORIZE_USER, _callbackMethodCall );
sharedHttpHelper.execute();
At the other a http class would post its result like so, where the caller has passed HTTP_AUTHORIZE_USER as mySubscriberChannel:
broker.publish( mySubscriberChannel, actualResult );
I'm aware, that I may use
someFutureObject.then( <doSomething> );
Actually, I feel that pub/sub better decouples the both sides of an async interaction. And it is more flexible when posting back results to different subscribers.
Could you explain, why someFutureObject.then() and custom code around that is better than pub/sub?
And probably supply some sample code, which replaces the pub/sub code using futures / then() in a better / standard way?
Or may I finally replace an async pattern by another async pattern and probably win nothing by doing so?
I think from your question, you're asking two things:
To make specific recommendations, I think I would need a lot more information. However, I can give you some general recommendations.
1: Future or callbacks
In this case, I would highly recommend taking the time to learn how futures and asynchronous methods work in Dart. You'll eventually come across something that needs to use them, so you might as well get it clear now. If you really can't figure it out, I'd suggest making a canned example and posting it to SO or asking on one of the dart/flutter channels.
That being said, a quick overview:
In dart, Future's are quite a lot like Promises in Javascript (I see you have at least some experience with JS). They perform an asynchronous computation, and then when they're done they call whatever callback you passed to then
.
However, in dart there are also the async
and await
keywords. If you mark a method async
, its signature must specify a return value of a Future. Within the function, if you have any methods you call that would return a Future, instead of doing future.then(callback...)
you can use await future
. I believe JS has something similar to this in one of its newest iterations.
From your question: someFutureObject.then( <doSomething> );
would become var result = await someFutureObject; <dosomething>;
If you look on the flutter website there are much more in-depth explanations of this.
2: Central vs Per-Widget
This depends a lot more on your specific situation. What I would do is categorize your asynchronous
interactions as things that a) happen once per run of the app, b) have an effect on global state, or c) are local to a specific part of the app.
For things that happen once per run, you can probably get away with simply performing them once with a loading screen (or while the splash is still up) - an example could be something like loading general settings from a remote server.
For things that have an effect on global state (i.e. logging in & out), I would suggest using InheritedWidgets and a corresponding StatefulWidget. This way, you can simply use the inherited widget at a lower level and use the various relevant properties, with the assurance that your widget will rebuild if the properties change.
For things that are local to a particular part of the app, I would suggest using FutureBuilder as it helps with things like showing a different widget while loading.
In terms of the logic for doing the actual http calls or whatever, you are probably best off making classes specific to the various API's you use. If there is some reason that you need to keep track of things specific to http calls, you can use either the Singleton or Registry pattern to make your API call classes able to hold state.
Unless you have a very specific reason, there probably isn't really any need for a pub/sub system; Although if you absolutely feel you need to use one, look at the eventbus plugin...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments