onChange method
Handles automatic UI updates based on state changes.
This method is automatically called whenever the cubit state changes, providing automatic UI management for loading states, error handling, and dialog presentation.
Automatic Behavior
- Loading State: Shows loading dialog automatically
- Error State: Shows error dialog with error information
- Success/Initial State: Dismisses any open dialogs
- State Transitions: Handles all state change scenarios
Dialog Management
- Loading Dialog: Presented when entering loading state
- Error Dialog: Presented when entering error state
- Dialog Cleanup: Automatic dismissal when leaving states
- Context Safety: Uses SDK navigator context for consistency
Localization
Error dialogs automatically use the current SDK locale setting for proper internationalization support.
Error Handling
- Combines error messages and error lists for comprehensive error display
- Provides fallback error messages when detailed information is unavailable
- Automatically resets state when errors are acknowledged
Post-Frame Callbacks
Uses WidgetsBinding.instance.addPostFrameCallback
to ensure
proper widget lifecycle management when showing dialogs.
Implementation
@override
void onChange(Change<ICubitState<G>> change) {
super.onChange(change);
change.nextState.whenOrNull(
loading: () {
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
showDialog(
context:
AmwalSdkNavigator.amwalNavigatorObserver.navigator!.context,
builder: (_) => const LoadingDialog(),
);
});
},
error: (err, msgList) {
_dismissDialog(change);
String errorMessage = '';
if (msgList?.isEmpty ?? true) {
errorMessage = err ?? 'Something Went Wrong';
} else {
errorMessage =
(msgList?.join(',') ?? err?.toString() ?? 'Something Went Wrong');
}
if (AmwalSdkNavigator.amwalNavigatorObserver.navigator != null) {
return showDialog(
context:
AmwalSdkNavigator.amwalNavigatorObserver.navigator!.context,
builder: (_) => Localizations(
locale: AmwalSdkSettingContainer.locale,
delegates: const [
...AppLocalizationsSetup.localizationsDelegates,
],
child: ErrorDialog(
locale: AmwalSdkSettingContainer.locale,
title: err ?? '',
message: errorMessage,
resetState: _resetState,
),
),
);
}
},
initial: () {
_dismissDialog(change);
},
success: (_) {
_dismissDialog(change);
},
);
}