fluttercommunity / get_it

Get It - Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App. Maintainer: @escamoteur
https://pub.dev/packages/get_it
MIT License
1.36k stars 149 forks source link

[Request] Add custom scope open/closing #313

Closed Anfet closed 1 year ago

Anfet commented 1 year ago

Hi everyone.

With this feature we can tie scopes to stateful widget lifecycle initState/dispose methods.

With current model it's not possible, since widget A can be disposed after Widget B initState is called. Therefor breaking scope destruction.

subzero911 commented 1 year ago

Same here, I just got a scope race condition, when I pop scope on widget dispose and register a new singleton on a new screen open, but it gots disposed in a several milliseconds. I think I'll file a new issue.

escamoteur commented 1 year ago

Any idea how this should lool like? If you only want to tie a scope to a widgets life cycle, the get_it_mixin would be the more elegant solution.

Anfet commented 1 year ago

Just add a scope name to close function.

escamoteur commented 1 year ago

Can you elaborate on how you mean that?

Anfet commented 1 year ago

Just add a scopeName to popScope function.

getIt.pushNewScope(scopeName: someScreen);
getIt.popScope(scopeName: someScreen);

This way we can close any open scope.

escamoteur commented 1 year ago

but that wouldn't be popping but removing a scope anywhere in the stack, or not?

There is actually a PR waiting for that https://github.com/fluttercommunity/get_it/pull/292

I'm not sure if that would help with the race condition that you observed?

Anfet commented 1 year ago

Yep. Thats exactly what is needed. Thank you.

escamoteur commented 1 year ago

https://github.com/fluttercommunity/get_it/issues/316#issuecomment-1535797647