Dependency<T> class

The Dependency class allows you to retrieve any dependency previously injected:

final repository = Dependency.get<MyRepository>(context);

The Dependency class allows you to inject many dependencies at once within the InjectAll widget:

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    // Inject multiple dependencies
    return InjectAll(
      dependencies: [
        Dependency<MyApi>((context) => MyApiImpl()),
        Dependency<MyRepository>((context) => MyRepositoryImpl(
          // You can depend on any dependency previsouly injected
          api: Dependency.get<MyApi>(context),
        )),
      ],
      builder: (context) {
        // Then retrieve your dependencies anywhere in the widget tree
        final repository = Dependency.get<MyRepository>(context);
        return MyView();
      },
    );
  }
}

Constructors

Dependency(T factory(BuildContext), {bool override = false, bool useExistingInstance = false})
The constructor of our Dependency class

Properties

factory ↔ T Function(BuildContext)
The method which will return the instance of our dependency
latefinal
hashCode int
The hash code for this object.
no setterinherited
override bool
A boolean that indicates if this dependency will override children's ones
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
useExistingInstance bool
A boolean that indicates if a previously injected dependency with the same Type T can be reused
final

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

get<T>(BuildContext context) → T
Retrieve any previously injected dependency with the given Type T Note that an exception will be thrown if no dependency have been found
maybeGet<T>(BuildContext context) → T?
Retrieve any previously injected dependency with the given Type T Note that null will be returned if no dependency have been found