-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
feat(flutter_bloc): Expose optional dispose
parameter to RepositoryProvider
#3096
Comments
Hi @Luckey-Elijah 👋 This is a duplicate of #2085. Closing for now but feel free to comment if you have additional comments/questions. Repositories should not be coupled with the application lifecycle and generally should not manage the lifecycle of things like http clients. |
Thank you, I would mostly agree with what is described here and the other referenced issue. However:
Yet in a majority of examples I have found, the repository seems to take responsibility for the lifecycle of a client (please see provided examples of flutter_login and fluttersaurus). Could you provide a reference to an example that would inject some HTTP client (or any other "disposable" object) into a repository and properly dispose the client. Thanks, again. |
In both of those cases the repositories are global and the http client instance is injected into the repo so it’s not the repository’s responsibility to close the instance. Also, in both cases the http client is used globally so the only time it would make sense to dispose it is when the application is detached but I’m also not sure if that’s really necessary since it should be garbage collected when the app has exited. Is there an example of a global http client being closed that you’re referring to? Thanks! |
Thanks for educating me 😄 But I'm not sure I quite follow.. With the fluttersaurus example, there is an inject-able HTTP client, but a client instance is never is actually injected; hence my confusion of the responsibility of the client's lifecycle since
I don't have an example in mind, perhaps I can conjure up one if needed. |
I’ll take a closer look at fluttersaurus I don’t recall exactly how it was implemented and I’m away from the computer now. Will take a look in a bit and get back to you 👍 |
Thank you very much! |
@Luckey-Elijah it is indeed using an optional http client which defaults/fallbacks to an internal instance if one is not passed in from the outside. This is a pattern often used since it helps with reducing the DI boilerplate, while also allowing for passing a concrete instance if needed(e.g.: a mocked instance for testing). As long as you don't need to explicitly call in a dispose method there's not really a need to explicitly instantiate it from the outside, although that's perfectly fine to do, especially to inject that http client instance into multiple api clients. |
Description
Repositories often have HTTP clients, stream controllers, or other objects that need to be disposed, canceled, or closed. Given a rule of thumb: whoever is creating the object should dispose the object.
Given that many use cases with
flutter_bloc
'sRepositoryProvider
create a repository at a global/top-level, the provider here should also provide a way top dispose a create repository.Desired Solution
Current implementation:
Proposed change:
Alternatives Considered
I have seen several examples of a Bloc or Cubit taking the responsibility to execute a dispose, cancel, or close method on a repository. For example:
flutter_login
authentication bloc. Or a repository never has a repository client closed at:fluttersaurus
#10Additional Context
N/A
The text was updated successfully, but these errors were encountered: