-
-
Notifications
You must be signed in to change notification settings - Fork 153
how to use the new feature of scope #109
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
Comments
Sorry, no examples besides my professionaö apps yet and they aren't public. |
I think, the docs should include example. |
Check out the tests for it https://github.com/fluttercommunity/get_it/blob/master/test/scope_test.dart |
@escamoteur thanks for the test examples, it really helps to understand how scope works. In the context of flutter, I feel like there could be a potential problem while developing. Imagine we have tabbarviews keeping the pages always alive, and we might want to create a scope for each page. This might result in the scope created in one page that sit underneath the scope of another page in the scope hierarchy and possibly not getting the right instance from GetIt. Is this a valid scenario? Would you give any suggestions on how to design the structure? Thanks |
if you want to use it on a tabbarview base and not one level above you are absolutely right. What you could do is create separate GetIt instances and register them one scope aboveinside the main GetIt. |
Please correct me if I'm wrong, do you mean create separate GetIt instances using |
no, there is no Method for this but you could do it this way GetIt.I.pushcope();
GetIt.I<GetIt>registerSingleton(GetIt.asNewInstance(), instanceName: 'TabOne', dispose: (x) -> x.reset() );
GetIt.I<GetIt>registerSingleton(GetIt.asNewInstance(), instanceName: 'TabOn2', dispose: (x) -> x.reset() );
GetIt.I<GetIt>registerSingleton(GetIt.asNewInstance(), instanceName: 'TabOn3', dispose: (x) -> x.reset() );
/// then you access it
GetIt.I<GetIt>(instanceName:'TabOne')<MyType>(); I have to admit, that's quite cumbersome especially you have to remember which GetIt instance you have to use where. |
I have an idea but I'm not sure if this will solve your needs. I could add a function
If you pop the scope all sidescopes will be popped too. What do you think? |
are the side scopes on the same level and independent of each other so one doesn't overwrite the other? And how does pop scope removes all sidescopes? It is not very clear. |
That would be the idea, but you have to switch dem manually |
If I pop one side scope, can others survive? This seems more flexible. |
what if its the original scope before you created the side scopes? I think that should then pop all of them? |
That makes sense. |
Will try tomorrow to make a beta |
For now I won't add horizontal scoping. I think if you really need this bound to the widget tree maybe provider is the better solution. |
I spent some weeks with flutter_modular because they support Service Locator, combine with modules (or scope in GetIt) and routes. But I found that they are not doing a good job to manage routes settings. Then I found GetIt V5 with scope. Let's see how will it go! |
thanks a lot. did you already see the compangion package get_it_mixin? |
I don't know it 😄, thanks for the info! |
@nguyenxndaidev I'd like to know if you were able to make it work. I'm already using getIt, but I'm missing having scoped dependencies for features that contain inner screens. The thing is that I'm not so sure at what time/where I should push a new scope. I just want to avoid doing that for each place I create a new screen. Maybe inside of it, but then we could not use constructor params I believe. |
Hi, I am currently not full-time on this, so the progress is slow. I currently still implementing the navigation to follow the way of angular router. My idea is to have a lifecycle of the routing. And plan to create and dispose scope here. I will give you info when I have time to continue this. |
Ok, one way that you can do is if you use a Statefulwidget, push the scope in ´initState´and pop it in Dispose. In general it depends what sort of Objects you register if it makes sense to push and pop at every screen. |
Thanks @escamoteur. With this solution it wouldn't be possible to insert the dependencies on the widget's constructor, right? I suppose it would only work if I obtained the dependencies in a lazy way. |
How do you mean that? |
Right now we're using getIt to provide our stateful widget and its dependencies, such as: class MyScreen extends StatefulWidget {
final ViewModel viewModel;
MyScreen(this.viewModel);
...
} This way we could easily test the StatefulWidget if we wanted. If we'll be calling pushScope and register the dependencies on the |
ah, I understand, yes, that's true that would mean you have to initialize a real GetIt instance at the start of your tests |
although, not necessarily, you could still pass in your viewmodels as they are stored in a property of the Widget, you can access them in initState of the state. |
Yes, I made it works! But with the reaction of flutter team on navigator 2 high-level API, I am not going to continue my solution. Instead, I will provide feedback to flutter team to support the idea of navigation life-cycle. And with this, we can have a chance to inject/destroy instances. And for my project, I will use GetIt for this purpose for sure. |
I'm trying to understand the use case(s) for get it scoping. Are all the instances available or only one is since I'm registering a new instance after pushing a new scope? What would be good use cases for scoping with get_it? I'm trying to write a medium article about it and would love some further understanding about this topic, thanks. |
It is so great to see this new feature added. Can you also please provide some examples of how to use it? Unless I have missed something.
Cheers.
The text was updated successfully, but these errors were encountered: