updateNotifier<T extends Object> function
Future<void>
updateNotifier<T extends Object>({
- required LikeStateResponse response,
- BuildContext? context,
- Future<
void> onInit(- LikeState state
- Future<
void> onSuccess(- T data
- Future<
void> onError(- LikeError error
- Future<
void> onException(- String message
- bool disableLoadingToast = true,
- bool disableSuccessToast = false,
- bool disableErrorToast = false,
- bool disableExceptionToast = false,
- bool disableCancelledToast = true,
- bool enableHaptics = true,
- Map<
LikeState, String> ? messageOverrides,
updateNotifier is the "Full" action handler. It provides built-in toasting, haptics, and state callbacks.
Use this for primary actions where you want automatic user feedback.
Implementation
Future<void> updateNotifier<T extends Object>({
required LikeStateResponse<dynamic> response,
BuildContext? context,
Future<void> Function(LikeState state)? onInit,
Future<void> Function(T data)? onSuccess,
Future<void> Function(LikeError error)? onError,
Future<void> Function(String message)? onException,
// Toast Control Flags
bool disableLoadingToast = true,
bool disableSuccessToast = false,
bool disableErrorToast = false,
bool disableExceptionToast = false,
bool disableCancelledToast = true,
bool enableHaptics = true,
Map<LikeState, String>? messageOverrides,
}) async {
if (onInit != null) await onInit(response.state);
T? castData(dynamic data) {
if (data == null) return null;
if (data is T) return data;
try {
return data as T;
} catch (_) {
return null;
}
}
String resolveMessage(String defaultMsg, LikeState state) {
return messageOverrides != null && messageOverrides.containsKey(state)
? messageOverrides[state]!
: defaultMsg;
}
switch (response.state) {
case LikeState.idle:
break;
case LikeState.loading:
if (!disableLoadingToast) {
if (context != null && !context.mounted) return;
LikeToastManager.showLoadingToast(
context: context,
title: 'Loading',
message: resolveMessage(response.message, LikeState.loading),
);
}
break;
case LikeState.success:
case LikeState.refreshing:
case LikeState.staleWhileRevalidate:
if (onSuccess != null) {
final data = castData(response.data);
if (data != null) await onSuccess(data);
}
if (response.state == LikeState.success && !disableSuccessToast) {
if (enableHaptics) HapticFeedback.lightImpact();
if (context != null && !context.mounted) return;
LikeToastManager.showToast(
context: context,
message: resolveMessage(response.resolvedMessage, LikeState.success),
type: ToastificationType.success,
);
}
break;
case LikeState.error:
final error =
response.error ??
LikeError(
message: response.message,
type: response.errorType ?? LikeApiErrorType.unknown,
code: response.code,
);
await onError?.call(error);
if (!disableErrorToast) {
if (enableHaptics) HapticFeedback.mediumImpact();
if (error.type != LikeApiErrorType.cancelled ||
!disableCancelledToast) {
if (context != null && !context.mounted) return;
LikeToastManager.showToast(
context: context,
message: resolveMessage(response.message, LikeState.error),
type: ToastificationType.warning,
);
}
}
break;
case LikeState.exception:
await onException?.call(response.message);
if (!disableExceptionToast) {
if (enableHaptics) HapticFeedback.heavyImpact();
if (context != null && !context.mounted) return;
LikeToastManager.showToast(
context: context,
message: resolveMessage(response.message, LikeState.exception),
type: ToastificationType.error,
);
}
break;
}
}