dependencies_flutter 0.1.6 copy "dependencies_flutter: ^0.1.6" to clipboard
dependencies_flutter: ^0.1.6 copied to clipboard

A simple and modular dependency injection system for Flutter.

example/example.dart

import 'package:dependencies_flutter/dependencies_flutter.dart';
import 'package:flutter/material.dart';
import 'package:dependencies/dependencies.dart';

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

class MyModule implements Module {
  @override
  void configure(Binder binder) {
    binder
      ..bindSingleton(Object());
  }
}

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

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

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

class YetAnotherWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MyBinder(
      child: Container(),
    );
  }

}
2
likes
40
pub points
29%
popularity

Publisher

unverified uploader

A simple and modular dependency injection system for Flutter.

Repository (GitHub)
View/report issues

License

Apache-2.0, BSD-3-Clause (license)

Dependencies

dependencies, flutter, quiver

More

Packages that depend on dependencies_flutter