avoid-functions-in-register-singleton
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() {
GetIt.I.registerSingleton(() => SomeClass()); // LINT
GetIt.instance.registerSingleton(() => SomeClass()); // LINT
final getIt = GetIt.instance;
getIt.registerSingleton(() => SomeClass()); // LINT
}
class SomeClass {}
✅ Good:
void main() {
GetIt.I.registerLazySingleton(() => SomeClass());
GetIt.I.registerFactory(() => SomeClass());
GetIt.I.registerSingleton(SomeClass());
}
class SomeClass {}