create method

  1. @override
void create({
  1. required bool didChangeDependency,
})
override

Initialize a provider.

This function must call setState or throw (or both).

Exceptions within this function will be caught and set the provider in error state. Then, reading this provider will rethrow the thrown exception.

  • didChangeDependency can be used to differentiate a rebuild caused by watch from one caused by refresh/invalidate.

Implementation

@override
void create({required bool didChangeDependency}) {
  final provider = this.provider as AsyncNotifierProviderBase<NotifierT, T>;

  final notifierResult = _notifierNotifier.result ??= Result.guard(() {
    return provider._createNotifier().._setElement(this);
  });

  // TODO test notifier constructor throws -> provider emits AsyncError
  // TODO test notifier constructor throws -> .notifier rethrows the error
  // TODO test notifier constructor throws -> .future emits Future.error
  notifierResult.when(
    error: (error, stackTrace) {
      onError(AsyncError(error, stackTrace), seamless: !didChangeDependency);
    },
    data: (notifier) {
      handleFuture(
        () => provider.runNotifierBuild(notifier),
        didChangeDependency: didChangeDependency,
      );
    },
  );
}