Search packages, repositories and app categories

Package detailsv4.0.0

injector

injector is a simple to use and lightweight dependency injection library for dart

AndroidiOSWebmacOSWindowsLinux
30-day downloads
928K
Likes
63
Pub points
160

Install package

flutter pub add injector

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

Injector is a lightweight and easy-to-use dependency injection library for Dart, designed to simplify the management of dependencies in Flutter applications. It provides a clean, minimal API for registering and resolving dependencies, supporting both singleton and transient instances. With seamless integration into Flutter projects, it enhances code modularity and testability. The library is ideal for developers seeking a straightforward alternative to more complex DI solutions without sacrificing functionality.

Use cases

  • Simple dependency management in Flutter apps
  • Testing with mocked dependencies
  • Modular application architecture
  • Reducing boilerplate in service instantiation
  • Streamlining state management setup
  • Enabling loose coupling between components

Key features

  • Minimal and intuitive API
  • Support for singleton and transient bindings
  • Easy integration with Flutter
  • Built-in support for constructor injection
  • Lightweight footprint
  • Comprehensive documentation

Suitable for

  • Beginner to intermediate Flutter developers
  • Small to medium-sized applications
  • Projects prioritizing simplicity over advanced features
  • Teams looking for quick DI implementation
  • Applications requiring testable and maintainable code

Considerations

  • Limited advanced features compared to full-featured DI libraries
  • Less suitable for very large-scale applications with complex dependency graphs
  • No built-in support for tree-based scopes or hierarchical dependency resolution
  • May require manual management of lifecycle in complex scenarios

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Injector 💉 Build Status

Injector is a simple dependency injection lib for Dart.

It does not replace a complex dependency injection framework like Dagger, but it provides the basics that most apps need. Feature requests are welcomed!

Internally the injector is a singleton that stores instances and builders in a Map.

Use registerDependency<Type>() to register the dependency. Never ever try to register or get dependencies without the generic type! Dart allows it, but we don't ;)

All instances are lazy loaded, meaning that at the time you request the dependency the instance is created.

Get your dependency using get<Type>().

Usage

A simple usage example:

// Register a dependency (Every time a new instance)
injector.registerDependency<Car>(() {
      var engine = injector.get<Engine>();
      var fuel = injector.get<Fuel>();
      var driver = injector.get<Driver>();
      
      return CarImpl(engine,fuel,driver);
    });
    
// Register a singleton
injector.registerSingleton<Car>(() {
          var engine = injector.get<Engine>();
          var fuel = injector.get<Fuel>();
          var driver = injector.get<Driver>();
          
          return CarImpl(engine,fuel,driver);
        });
        
 // Use custom Factories by extending [Factory].
 injector.register(CustomFactory(() => Engine()));
    
// Register your dependencies / singletons in the  __main.dart__ file

Notice

If you have an issue with getting dependencies from the injector, check that imports of your dependencies classes are absolute.

Good: import 'package:<project_name>/some/package/<file_name>.dart';

Bad: import '../../some/package/<file_name>.dart';

Features and bugs

Please file feature requests and bugs at the issue tracker.

Categories

Source repository

Projects using this package