Skip to main content

avoid-functions-in-register-singleton

added in: 1.10.0
Pro+

Warns when a function is passed to registerSingleton.

Passing a function may produce unexpected results. Consider passing an object instance, or use registerFactory or registerLazySingleton instead.

Example

❌ Bad:

void main() {
// LINT: Passing a function to "registerSingleton" may produce unexpected results. Try passing an object instance, or use "registerFactory" or "registerLazySingleton" instead.
GetIt.I.registerSingleton(() => SomeClass());

// LINT: Passing a function to "registerSingleton" may produce unexpected results. Try passing an object instance, or use "registerFactory" or "registerLazySingleton" instead.
GetIt.instance.registerSingleton(() => SomeClass());

final getIt = GetIt.instance;
// LINT: Passing a function to "registerSingleton" may produce unexpected results. Try passing an object instance, or use "registerFactory" or "registerLazySingleton" instead.
getIt.registerSingleton(() => SomeClass());
}

class SomeClass {}

✅ Good:

void main() {
GetIt.I.registerLazySingleton(() => SomeClass());
GetIt.I.registerFactory(() => SomeClass());
GetIt.I.registerSingleton(SomeClass());
}

class SomeClass {}