ProvideSingleton class

Marks a method in a module as a singleton provider.

A convenience annotation that is equivalent to @Provide(shared: true). This is the recommended way to explicitly mark singleton providers in modules.

Async Support: Provider methods can return Future<T> for async initialization.

Example - Basic singleton provider:

@Module()
class CoreModule {
  @ProvideSingleton()
  DatabaseConnection createConnection() => DatabaseConnection();

  @ProvideSingleton(name: 'mainCache')
  Cache createCache() => MemoryCache();
}

// Generated code:
builder.registerSingleton<DatabaseConnection>(
  (i) => i<CoreModule>().createConnection(),
);
builder.registerSingleton<Cache>(
  (i) => i<CoreModule>().createCache(),
  name: 'mainCache',
);

Example - Async singleton provider:

@Module()
class DatabaseModule {
  @ProvideSingleton()
  Future<Database> createDatabase() async {
    final db = Database();
    await db.connect();
    return db;
  }
}

// Generated code:
builder.registerAsyncSingleton<Database>(
  (i) async => await i<DatabaseModule>().createDatabase(),
);

// Usage:
final db = await injector.getAsync<Database>();
Inheritance

Constructors

ProvideSingleton({String? name, String? env, int? priority})
const

Properties

env String?
The environment in which this provider should be registered.
finalinherited
hashCode int
The hash code for this object.
no setterinherited
name String?
A unique name for this provider registration.
finalinherited
priority int?
The registration priority for this provider.
finalinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
shared bool
Whether the provided service should be shared (singleton) or created fresh each time (factory).
finalinherited

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