didUpdateProvider method

  1. @override
void didUpdateProvider(
  1. ProviderObserverContext context,
  2. Object? previousValue,
  3. Object? newValue
)

Called by providers when they emit a notification.

  • newValue will be null if the provider threw during initialization.
  • previousValue will be null if the previous build threw during initialization. mutation If the change is caused by a "mutation", [] will be the invocation that caused the state change. This includes when a mutation manually calls state=:
@riverpod
class Example extends _$Example {
  @override
  int count() => 0;

  @mutation
  int increment() {
    state++; // This will trigger `didUpdateProvider` and "mutation" will be `#increment`

    // ...
  }
}

Implementation

@override
void didUpdateProvider(
  ProviderObserverContext context,
  Object? previousValue,
  Object? newValue,
) {
  String valueStr;
  try {
    valueStr = newValue.toString();
    if (valueStr.length > 256) valueStr = '${valueStr.substring(0, 256)}...';
  } catch (_) {
    valueStr = newValue.runtimeType.toString();
  }

  unawaited(
    _storage.addSimpleLog(
      SimpleOverlayLog(
        timestamp: DateTime.now(),
        tag: context.provider.name ?? context.provider.runtimeType.toString(),
        level: LogLevel.debug,
        message: 'Updated: $valueStr',
      ),
    ),
  );
}