Since I did not find an answer to this question in the documentation, I must ask here :)
Question:
I am creating class/service that will handle http requests, I will use Dio library, but I have a concern regarding the performance, do I need to declare dio object as static instance in order to save some resources? Is It necessary?
For example:
static const Dio http = Dio();
And do I have to call close() method every time I finished with http request?
Please advice.
Thanks,
Lazar
Since I did not find an answer to this question in the documentation, I must ask here :)
Question:
I am creating class/service that will handle http requests, I will use Dio library, but I have a concern regarding the performance, do I need to declare dio object as static instance in order to save some resources? Is It necessary?For example:
static const Dio http = Dio();And do I have to call close() method every time I finished with http request?
Please advice.
Thanks,
Lazar
I'm just taking a look at this plugin, so I didn't read the documentation, but in Flutter you can instantiate anything using factory.
An example:
class CustomDio {
static CustomDio _singletonHttp;
Dio _http;
factory CustomDio() {
if (_singletonHttp == null) singletonHttp = CustomDio.();
return singletonHttp;
}
CustomDio.() {
_http = Dio();
}
get client => _http;
dispose() {
_httpClient.close();
}
}
I built this class out of the data from your question, I don't know if it's objectively what you want, but it's learning anyway.
CustomDio().client will always call the same instance in this example, and will take advantage of the keep alive of your server.
Even using http and thinking of using Dio for the first time today, to my knowledge you should close the http connection when no more requests are made, or when a downtime limit is reached.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.
Since I did not find an answer to this question in the documentation, I must ask here :)
Question:
I am creating class/service that will handle http requests, I will use Dio library, but I have a concern regarding the performance, do I need to declare dio object as static instance in order to save some resources? Is It necessary?For example:
static const Dio http = Dio();And do I have to call close() method every time I finished with http request?
Please advice.
Thanks,
Lazar
Any news if is correct to create a singleton?
@Allan-Nava Yes, it is correct in my opinion. Already used singleton for my production apps and it's working fine.
Most helpful comment
@Allan-Nava Yes, it is correct in my opinion. Already used singleton for my production apps and it's working fine.