ViewStateListener<T, B extends BlocBase<ViewState<T>>> constructor

ViewStateListener<T, B extends BlocBase<ViewState<T>>>({
  1. Key? key,
  2. B? bloc,
  3. BlocListenerCondition<ViewState<T>>? listenWhen,
  4. LoadingCallback? onLoading,
  5. RefreshingCallback<T>? onRefreshing,
  6. @Deprecated('This callback will removed. Use "onData" instead.') SuccessCallback<T>? onSuccess,
  7. DataCallback<T>? onData,
  8. EmptyCallback? onEmpty,
  9. ErrorCallback? onError,
  10. Widget? child,
})

Implementation

ViewStateListener({
  super.key,
  super.bloc,
  super.listenWhen,
  LoadingCallback? onLoading,
  RefreshingCallback<T>? onRefreshing,
  @Deprecated('This callback will removed. Use "onData" instead.')
  SuccessCallback<T>? onSuccess,
  DataCallback<T>? onData,
  EmptyCallback? onEmpty,
  ErrorCallback? onError,
  super.child,
})  : assert(
        !(onSuccess != null && onData != null),
        'The onSuccess and onData callbacks should NOT be used together. The onSuccess callback is deprecated and can be safely removed.',
      ),
      super(
        listener: (BuildContext context, ViewState<T> state) {
          return switch (state) {
            Initial<T>() => () {},
            Loading<T>() => onLoading?.call(context),
            Refreshing<T>(value: final value) =>
              onRefreshing?.call(context, value),
            Data<T>(value: final value) =>
              (onData ?? onSuccess)?.call(context, value),
            Empty<T>() => onEmpty?.call(context),
            Failure<T>(error: final value) => onError?.call(context, value)
          };
        },
      );