Injectable class

Compile-time metadata that marks a class Type or Function for injection.

The @Injectable() annotation has two valid uses:

  1. On a class Type
  2. On a top-level Function

Use #1: A class Type

The class must be one of the following:

  • non-abstract with a public or default constructor
  • abstract but with a factory constructor

A class annotated with @Injectable() can have only a single constructor or the default constructor. The DI framework resolves the dependencies and invokes the constructor with the resolved values.


// Use the default constructor to create a new instance of MyService.
class MyService {}

// Use the defined constructor to create a new instance of MyService.
// Each positional argument is treated as a dependency to be resolved.
class MyService {
  MyService(Dependency1 d1, Dependency2 d2)

// Use the factory constructor to create a new instance of MyServiceImpl.
abstract class MyService {
  factory MyService() => new MyServiceImpl();

Use #2: A top-level Function

The Injectable() annotation works with top-level functions when used with useFactory.


// Could be put anywhere DI providers are allowed.
const Provide(MyService, useFactory: createMyService);

// A `Provide` may now use `createMyService` via `useFactory`.
MyService createMyService(Dependency1 d1, Dependency2 d2) => ...




hashCode → int
The hash code for this object. [...]
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited


noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
toString() → String
Returns a string representation of this object.


operator ==(other) → bool
The equality operator. [...]