debugAssertSingletonRegistration<T extends Object> function

void debugAssertSingletonRegistration<T extends Object>(
  1. GetIt di
)

Verifies that T is registered with a lifetime the page family supports.

Only singleton and lazy-singleton registrations are valid for a page's state manager. A factory hands every resolution its own instance, and since pages provide the instance with BlocProvider.value and never close it, nothing would ever dispose those instances — GetIt does not track them.

Runs only in debug builds; the body is compiled out of release.

Implementation

void debugAssertSingletonRegistration<T extends Object>(GetIt di) {
  assert(() {
    if (!di.isRegistered<T>()) {
      return true;
    }
    // GetIt has no public lifetime query, so probe it: two resolutions of a
    // factory return different instances, a singleton returns the same one.
    if (identical(di<T>(), di<T>())) {
      return true;
    }
    throw StateError(
      '$T is registered as a factory, which is not supported for a page\'s '
      'state manager.\n'
      'Pages provide it with BlocProvider.value and never close it, so a '
      'factory instance would leak on every page build.\n'
      'Register it with registerSingleton/registerLazySingleton instead '
      '(or @singleton / @lazySingleton with injectable).',
    );
  }());
}