read property

T read

returs always the same instance of T, if it is not created yet this will create it.

Implementation

T get read {
  // if the notifier was created before
  if (_mounted) {
    return ProviderScope.instance.containers[hashCode]!.notifier as T;
  }

  // check if we have a previous reference
  _ref ??= ProviderReference(providerDisposeCallback: _dispose);

  // create a new Notifier
  final notifier = _overriddenCreator != null
      ? _overriddenCreator!(_ref!)
      : _creator(_ref!);

  // save the notifier into containers
  ProviderScope.instance.containers[hashCode] = ProviderContainer(
    providerHashCode: hashCode,
    notifier: notifier as BaseNotifier,
    reference: _ref!,
    autoDispose: _overriddenAutoDispose ?? _autoDispose,
    routeName: BaseProvider.creatorName,
  );
  _mounted = true;
  return notifier;
}