Skip to content

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

Closed
PandaGeek1024 opened this issue Sep 20, 2020 · 28 comments
Closed

how to use the new feature of scope #109

PandaGeek1024 opened this issue Sep 20, 2020 · 28 comments

Comments

@PandaGeek1024
Copy link

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.

@escamoteur
Copy link
Collaborator

Sorry, no examples besides my professionaö apps yet and they aren't public.
What do you want to know, I thought the docs should be easy to understand?

@Lzyct
Copy link

Lzyct commented Sep 21, 2020

I think, the docs should include example.
For example, maybe you can use my issue

@escamoteur
Copy link
Collaborator

@PandaGeek1024
Copy link
Author

PandaGeek1024 commented Sep 21, 2020

@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

@escamoteur
Copy link
Collaborator

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.
I use scopes more on a business logic level than directly connected to every widget.

@PandaGeek1024
Copy link
Author

Please correct me if I'm wrong, do you mean create separate GetIt instances using
GetIt.asNewInstance();
and register these instances to the global GetIt instance to create a link between all GetIt instances? Is there a method to do that?

@escamoteur
Copy link
Collaborator

escamoteur commented Sep 23, 2020

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.
Can you explain a bit in more detail how the usecase for this would look like?
Maybe I can come up with an easier solution. What I don't want to start is connecting GetIt instances to the widgt Tree via an inherited widget.
As said most of the time scopes are more closely connected to model state changes then necessarily from the UI.

@escamoteur
Copy link
Collaborator

I have an idea but I'm not sure if this will solve your needs. I could add a function

pushSideScope('Name')
and a function 
switchSideScope(scopeName)

If you pop the scope all sidescopes will be popped too.

What do you think?

@PandaGeek1024
Copy link
Author

PandaGeek1024 commented Oct 4, 2020

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.

@escamoteur
Copy link
Collaborator

That would be the idea, but you have to switch dem manually

@PandaGeek1024
Copy link
Author

If I pop one side scope, can others survive? This seems more flexible.

@escamoteur
Copy link
Collaborator

what if its the original scope before you created the side scopes? I think that should then pop all of them?

@PandaGeek1024
Copy link
Author

That makes sense.

@escamoteur
Copy link
Collaborator

Will try tomorrow to make a beta

@escamoteur
Copy link
Collaborator

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.

@xuanswe
Copy link

xuanswe commented Jan 7, 2021

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.
I am going to use GetIt scope with Flutter Navigation 2.0 to archive the same thing flutter_modular does.

Let's see how will it go!
Thanks for creating GetIt!

@escamoteur
Copy link
Collaborator

thanks a lot. did you already see the compangion package get_it_mixin?

@xuanswe
Copy link

xuanswe commented Jan 8, 2021

thanks a lot. did you already see the compangion package get_it_mixin?

I don't know it 😄, thanks for the info!

@douglasiacovelli
Copy link

@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.

@xuanswe
Copy link

xuanswe commented Jan 21, 2021

@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 want a flexible but simple solution. I also want smt default, so if not specify, the scope will be automatically managed with default behavior for each route group.

I will give you info when I have time to continue this.

@escamoteur
Copy link
Collaborator

Ok, one way that you can do is if you use a Statefulwidget, push the scope in ´initState´and pop it in Dispose.
If you use the get_it_mixin there is even a ´pushScope´method included that will automatically pop the scope the moment the widget gets killed. that works even in a StatelessWidget.

In general it depends what sort of Objects you register if it makes sense to push and pop at every screen.

@douglasiacovelli
Copy link

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.

@escamoteur
Copy link
Collaborator

How do you mean that?

@douglasiacovelli
Copy link

douglasiacovelli commented Jan 22, 2021

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 initState, this would mean we won't have them ready for the constructor of the Widget. Does it make sense?

@escamoteur
Copy link
Collaborator

ah, I understand, yes, that's true that would mean you have to initialize a real GetIt instance at the start of your tests

@escamoteur
Copy link
Collaborator

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.

@xuanswe
Copy link

xuanswe commented Feb 14, 2021

@nguyenxndaidev I'd like to know if you were able to make it work.

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.

@smunshimi
Copy link

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.

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

6 participants