get_it
get_it copied to clipboard
Add Provider/Injector widget to encapsulate Scope API
Currently, GetIt has the ability to define dependencies dynamically and restrict them to specific areas of the application using the scope API. It would be nice if there were a widget that abstracted this functionality. Here's what a very contrived usage would look like.
/// Used in some parent widget
GetItScope(
create: (instance) {
instance.registerFactory(() => SomeDependency());
instance.registerFactory(() => AnotherDependency());
},
child: SomePage(),
);
class SomePage extends StatelessWidget {
const SomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
final dep = locator.get<SomeDependency>();
return Text(dep.hello);
}
}
Internally, the widget would handle pushing and popping the scope.
Inspiration: https://twitter.com/chimon1984/status/1410347621277962240?s=20
Hi Ryan,
what would be the advantage to https://pub.dev/documentation/get_it_mixin/latest/get_it_mixin/GetItMixin/pushScope.html ?
So far get_it doesn't have a Flutter dependency which I did not want to change. So I would need to create a separate paclage for this.
Hi Ryan,
what would be the advantage to https://pub.dev/documentation/get_it_mixin/latest/get_it_mixin/GetItMixin/pushScope.html ?
So far get_it doesn't have a Flutter dependency which I did not want to change. So I would need to create a separate package for this.
Yes, ideally this functionality would live in a get_it_flutter
package. I don't really see this as an either/or solution, vs. having both, but I acknowledge that managing another package is more work. Technically speaking there are only two advantages that I can think of with having a widget version. 1) With compositional inheritance (a widget) I cannot accidentally override the functionality of pushScope
as I can with a mixin. 2) By using a widget, the interface can be made explicit, because if I am defining a new scope, the create method should be required.
Additionally (but unrelated) it would be worthwhile to update the code examples and ReadMe of get_it_mixin with code examples of how to use pushScope
.
I agree, a separate package would make sense for that. Also about the readme on this in the get_it_mixin. Maybe you could make a PR on that ;-)
I agree, a separate package would make sense for that. Also about the readme on this in the get_it_mixin. Maybe you could make a PR on that ;-)
I see what you did there :)
@chimon2000 seen this here https://github.com/escamoteur/watch_it/issues/1