autoDispose constant

AutoDisposeBlocProviderBuilder const autoDispose

Auto Dispose

Marks the provider as automatically disposed when no-longer listened.

final counterProvider1 = BlocProvider.autoDispose((ref) => CounterCubit(0));

final counterProvider2 - AutoDisposeBlocProvider((ref) => CounterCubit(0));

The maintainState property is a boolean (false by default) that allows the provider to tell Riverpod if the state of the provider should be preserved even if no-longer listened.

final myProvider = BlocProvider.autoDispose((ref) {
  final asyncValue = ref.watch(myFutureProvider);
  final firstState = asyncValue.data!.value;
  ref.maintainState = true;
  return CounterBloc(firstState);
});

This way, if the asyncValue has no data, the provider won't create correctly the state and if the UI leaves the screen and re-enters it, the asyncValue will be readed again to retry creating the state.

Implementation

static const autoDispose = AutoDisposeBlocProviderBuilder();