dependency_container 0.1.2
dependency_container: ^0.1.2 copied to clipboard

A simple flutter dependency container which is based on a Map, providing support for factories.

Dart style: lint

dependency container #

A simple flutter dependency container which is more or less a map and brings support for factory methods and lazy building of services.

Main goals #

  • be simple
  • support for easy testing and mocking

Getting Started #

Share app container deeper in the widget tree #

Flutter InheritedWidget #

Flutter Provider #

Example apps #

Async app_config.dart to init the AppContainer #

/// optional parameters for testing
/// enables the re-usage of this function for test and mocking
/// of the infrastructure dependencies
Future<AppContainer> buildContext([Future<SharedPreferences>?  pref]) async {
  pref ??= SharedPreferences.getInstance();
  final f = await pref;
  return AppContainer()
      .addFactory((container) => YourOtherService(container.get<YourPrefService>()));

Async init in the main.dart #

class MyHomePage extends StatefulWidget {
  /// Enable setting of the container for testing
  MyHomePage({Key? key, required this.title,
      Future<AppContainer>? container}) :
        _container = container ?? buildContext(),
        super(key: key);

  final String title;
  final Future<AppContainer> _container;

  _MyHomePageState createState() => _MyHomePageState();

class _MyHomePageState extends State<MyHomePage> {

  void dispose() {
    // dispose all beans
    widget._container.then((value) => value.close());

  Widget build(BuildContext context) {
    return FutureBuilder<AppContainer>(
      future: widget._container,
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          // your main screen
          return _buildMain(snapshot.requireData);
        } else {
          // your splash screen
          return Scaffold(
            appBar: AppBar(title: Text(widget.title)),
            body: const Center(child: CircularProgressIndicator())

  Widget _buildMain(AppContainer container) {
    final yourOtherService = container.get<YourOtherService>();
    final yourPrefService = container.get<YourPrefService>();
    return Scaffold(
        // your code
pub points


A simple flutter dependency container which is based on a Map, providing support for factories.

Repository (GitHub)
View/report issues


API reference




Packages that depend on dependency_container