Skip to content

marcguilera/dependencies_flutter.dart

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pub package

Simple package to ease the use of the dependencies with Flutter leveraging the power of InheritedWidget.

Read the Medium article

Usage

class SomeRootWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return InjectorWidget.bind(
      bindFunc: (binder) {
        binder
          ..install(MyModule())
          ..bindSingleton("api123", name: "api_key");
      },
      child: SomeWidget()
    );
  }
}

You can also extend BindingInjectorWidget to configure your dependencies:

class MyBinder extends ModuleWidget {
  MyBinder({Key key, @required Widget child}): super(key: key, child: child);
  @override
  void configure(Binder binder) {
    binder
      ..bindSingleton(Object());
  }
}

You can later refer to the injector like any other InheritedWidget.

class SomeWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final injector = InjectorWidget.of(context);
    final apiKey = injector.get(name: "api_key");
    return SomeContainerNeedingTheKey(apiKey);
  }
}

Or using the InjectorWidgetMixin:

class SomeWidget extends StatelessWidget with InjectorWidgetMixin {
  @override
  Widget buildWithInjector(BuildContext context, Injector injector) {
    final object = injector.get<Object>();
    print(object);
    return Container();
  }
}

About

A simple and modular dependency injection system for Flutter.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages