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

Question: Should Dio object created as singleton #613

Closed
lazarvgd opened this issue Dec 19, 2019 · 7 comments
Closed

Question: Should Dio object created as singleton #613

lazarvgd opened this issue Dec 19, 2019 · 7 comments

Comments

@lazarvgd
Copy link

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

@jonataslaw
Copy link

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.

@stale
Copy link

stale bot commented Feb 6, 2020

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.

@stale stale bot added the stale label Feb 6, 2020
@stale stale bot closed this as completed Feb 13, 2020
@Allan-Nava
Copy link

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?

@tijanirf
Copy link

@Allan-Nava Yes, it is correct in my opinion. Already used singleton for my production apps and it's working fine.

@cesc1802
Copy link

@Allan-Nava Yes, it is correct in my opinion. Already used singleton for my production apps and it's working fine.

can you share your code to create dio with singleton partten?

@RoBiN-HoOd-1
Copy link

@Allan-Nava Yes, it is correct in my opinion. Already used singleton for my production apps and it's working fine.

can you share your code to create dio with singleton partten?

you can just create is using get_it dependency injection package and use it in every file.. i am doing the same here's the code: getIt.I
..registerSingleton(Dio());

@naumanahmed19
Copy link

naumanahmed19 commented Nov 27, 2023

I have created this dio service please check here.

https://gist.github.com/naumanahmed19/9b5a42bb0eeeb7d0f6a76ac023a20eae

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

No branches or pull requests

7 participants