get_it icon indicating copy to clipboard operation
get_it copied to clipboard

Add Provider/Injector widget to encapsulate Scope API

Open chimon2000 opened this issue 3 years ago • 4 comments

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

chimon2000 avatar Jul 02 '21 04:07 chimon2000

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.

escamoteur avatar Jul 03 '21 13:07 escamoteur

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.

chimon2000 avatar Jul 05 '21 06:07 chimon2000

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 ;-)

escamoteur avatar Jul 09 '21 09:07 escamoteur

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 avatar Jul 09 '21 15:07 chimon2000

@chimon2000 seen this here https://github.com/escamoteur/watch_it/issues/1

escamoteur avatar May 08 '23 10:05 escamoteur