resolveAsyncField<T> method
Resolves the value of an asynchronous hidden field and updates its state.
- Parameter
fieldNameThe name of the field to resolve. - Returns: a Future<T> containing the resolved value or throws if the future fails.
- Throws: ArgumentError if the field doesn't exist or is not an async hidden field.
- Throws: Exception if the future fails to resolve.
Implementation
Future<T> resolveAsyncField<T>(String fieldName) {
final fieldState = state.asyncHiddenField<T>(fieldName);
return fieldState.pendingValue.then((resolvedValue) {
// Update the field with the resolved value and clear any previous error
state.fields[fieldName] =
fieldState.copyWith(value: resolvedValue).updateError(null);
return resolvedValue;
}).catchError((error, stack) {
log(
'⚠️ Hidden Async field $fieldName failed to resolve: $error',
stackTrace: stack,
);
state.fields[fieldName] = fieldState.updateError(error.toString());
throw error; // Re-throw so caller can catch
});
}