BlocxFormBloc<F extends BlocxBaseFormEntity<F, E> , P, E extends Enum> class
abstract
Base class for all form blocs in the blocx ecosystem.
Manages the full lifecycle of an immutable form backed by a BlocxBaseFormEntity subclass. Wires up validation, submission, optional stepped navigation, unique-field checking, and initial info-fetching automatically based on which mixins are applied.
Minimal setup
- Define a field enum
Eand a BlocxBaseFormEntity subclassF. - Extend BlocxFormBloc with those types plus a payload type
P. Usevoidwhen there is no initial payload. - Provide
submitUseCaseTask, the required submit task.
class CreatePostBloc extends BlocxFormBloc<PostForm, void, PostField> {
CreatePostBloc() : super(PostForm.empty());
@override
BlocxUseCaseTask<CreatePostInput, PostResponse> get submitUseCaseTask {
return BlocxUseCaseTask<CreatePostInput, PostResponse>(
useCase: _createPostUseCase,
inputBuilder: () => CreatePostInput(title: formData.title),
);
}
}
Optional features
| Mixin | Behaviour unlocked |
|---|---|
| BlocxFormSteppedMixin | Multi-step form navigation |
| BlocxUniqueFieldValidatorMixin | Async per-field uniqueness checks |
| BlocxFormInfoFetcherMixin | Fetches remote data before the form renders |
Update / edit forms
When editing an existing entity, pass the entity as the payload P and
override applyPayloadToFormData to hydrate F from it. Dispatch
BlocxFormEventInit with the payload to trigger hydration.
Type parameters
F: The immutable form entity. Must extend BlocxBaseFormEntity.P: The optional payload type used for edit-mode hydration. Usevoidfor create-only forms.E: The enum identifying each field. Used as the key for updates, validation errors, and info-fetching.
- Inheritance
-
- Object
- BaseBloc<
BlocxFormEvent, BlocxFormState< F, E> > - BlocxFormBloc
- Mixed-in types
-
- BlocxFormErrorsMixin<
F, P, E>
- BlocxFormErrorsMixin<
- Implementers
Constructors
- BlocxFormBloc(F formData)
-
Creates the bloc with the blank
formDataas the initial state.
Properties
- comesFromPreviousStep → bool
-
Whether the current step was reached by going backward.
no setter
- defaultError → ReadableError
-
The fallback ReadableError used when no
BlocxErrorTranslatoris registered or when the translator does not recognise the error.no setterinherited - emitChangesOnUpdate → bool
-
Whether every field update should emit an intermediate update state.
no setterinherited
- errorDisplayPolicy → ErrorDisplayPolicy
-
The default error display policy for this form bloc.
no setterinherited
-
errors
→ Map<
E, Set< String> > -
An immutable snapshot of the current field errors.
no setterinherited
-
fieldsFetchingInfo
→ Set<
E> -
The set of fields currently waiting on a remote info fetch.
no setter
- formData ↔ F
-
Current form data.
getter/setter pairinherited
- formValidationMode → FormValidationMode
-
The current validation mode.
no setterinherited
- hashCode → int
-
The hash code for this object.
no setterinherited
- isClosed → bool
-
Whether the bloc is closed.
no setterinherited
- isFormSubmittable → bool
-
Whether the current form can execute the submit use case.
no setter
- isInfoFetcher → bool
-
Whether this bloc has BlocxFormInfoFetcherMixin applied.
no setter
- isStepped → bool
-
Whether this bloc has BlocxFormSteppedMixin applied.
no setter
- isUniqueFieldValidator → bool
-
Whether this bloc has BlocxUniqueFieldValidatorMixin applied.
no setter
- isUpdate → bool
-
Whether the form is currently in update/edit mode.
no setterinherited
- payload → P?
-
The current update payload, if any.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- screenManagerCubit → ScreenManagerCubit
-
Direct access to the internal ScreenManagerCubit.
no setterinherited
-
state
→ BlocxFormState<
F, E> -
The current state.
no setterinherited
- stepIndex → int
-
Current form step index.
no setterinherited
-
stream
→ Stream<
BlocxFormState< F, E> > -
The current stream of states.
no setterinherited
-
submitUseCaseTask
→ BlocxUseCaseTask<
Object?, Object?> -
The use case task executed when the form is submitted.
no setterinherited
-
uniqueKeysBeingChecked
→ Set<
E> -
The set of fields whose uniqueness is currently being checked remotely.
no setter
- validateOnInit → bool
-
Whether the form should validate immediately after initialisation.
no setterinherited
Methods
-
add(
BlocxFormEvent event) → void -
Notifies the
Blocof a neweventwhich triggers all correspondingEventHandlerinstances.inherited -
addError(
Object error, [StackTrace? stackTrace]) → void -
Reports an
errorwhich triggersonErrorwith an optional StackTrace.inherited -
applyPayloadToFormData(
P payload) → FutureOr< F> -
Converts an update payload into form data.
inherited
-
clearAllErrors(
) → void -
Clears all field errors and cancels all pending timed-error timers.
inherited
-
clearFieldError(
E key, {String? errorMessage}) → bool -
Clears either one error message or all errors for the field
key.inherited -
clearFieldErrors(
BlocxFormEventClearFieldError< E> event, Emitter<BlocxFormState< emit) → FutureOr<F, E> >void> -
Handles BlocxFormEventClearFieldError.
inherited
-
clearTimers(
) → void -
Cancels all pending timed-error timers.
inherited
-
close(
) → Future< void> -
Closes the bloc and cancels all pending timed-error timers.
override
-
displayErrorSnackbar(
String message, {String? title}) → void -
Displays an error snackbar with
messageand optionaltitle.inherited -
displayErrorWidget(
ReadableError error) → void -
Displays a full-page error widget for
error.inherited -
displayErrorWidgetByErrorCode(
BlocXErrorCode errorCode, {Object? error, StackTrace? stackTrace}) → void -
Displays a full-page error widget derived from a BlocXErrorCode.
inherited
-
displayInfoSnackbar(
String message, {String? title}) → void -
Displays an info snackbar with
messageand optionaltitle.inherited -
displayWarningSnackbar(
String message, {String? title}) → void -
Displays a warning snackbar with
messageand optionaltitle.inherited -
doBeforeSubmit(
Emitter< BlocxFormState< emit) → Future<F, E> >bool> -
Runs before the submit use case.
inherited
-
emit(
BlocxFormState< F, E> state) → void -
emitis only for internal use and should never be called directly outside of tests. TheEmitterinstance provided to eachEventHandlershould be used instead.inherited -
emitState(
Emitter< BlocxFormState< emit) → voidF, E> > - Emits the current loaded form state.
-
handleError(
Object error, Emitter< BaseState> emit, {StackTrace? stacktrace}) → FutureOr<void> -
Logs
error, translates it to a ReadableError, then surfaces it according to errorDisplayPolicy.inherited -
handleUpdateFormDataEvent(
BlocxFormEventUpdateFormData< P> event, Emitter<BlocxFormState< emit) → FutureOr<F, E> >void> -
Handles replacing the full form data from a new payload.
inherited
-
hasError(
E key, [String? code]) → bool -
Whether the field identified by
keycurrently has an error.inherited -
initData(
F formData) → void -
Initialises form data and registers form data event handlers.
inherited
-
initErrors(
) → void -
Initialises form error event handlers.
inherited
-
initForm(
BlocxFormEventInit< P> event, Emitter<BlocxFormState< emit) → FutureOr<F, E> >void> -
Handles the initial form event.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
on<
E extends Event> (EventHandler< E, BlocxFormState< handler, {EventTransformer<F, E> >E> ? transformer}) → void -
Register event handler for an event of type
E. There should only ever be one event handler per event typeE.inherited -
onChange(
Change< BlocxFormState< change) → voidF, E> > -
Called whenever a
changeoccurs with the givenchange. Achangeoccurs when a newstateis emitted.onChangeis called before thestateof thecubitis updated.onChangeis a great spot to add logging/analytics for a specificcubit.inherited -
onDone(
BlocxFormEvent event, [Object? error, StackTrace? stackTrace]) → void -
Called whenever an
eventhandler for a specificBlochas completed. This may include anerrorandstackTraceif an uncaught exception occurred within the event handler.inherited -
onError(
Object error, StackTrace stackTrace) → void -
Called whenever an
erroroccurs and notifiesBlocObserver.onError.inherited -
onEvent(
BlocxFormEvent event) → void -
Called whenever an
eventisadded to theBloc. A great spot to add logging/analytics at the individualBloclevel.inherited -
onFormSubmitted(
Emitter< BlocxFormState< emit, BlocxUseCaseResult<F, E> >Object?> result) → Future<bool> -
Runs after successful form submission.
inherited
-
onTransition(
Transition< BlocxFormEvent, BlocxFormState< transition) → voidF, E> > -
Called whenever a
transitionoccurs with the giventransition. Atransitionoccurs when a neweventis added and a new state isemittedfrom a correspondingEventHandler.inherited -
pop(
) → void -
Triggers a pop/back-navigation signal.
inherited
-
setErrorToField(
BlocxFormEventSetErrorToField< E> event, Emitter<BlocxFormState< emit) → FutureOr<F, E> >void> -
Handles BlocxFormEventSetErrorToField.
inherited
-
setFieldError(
E key, String error) → bool -
Adds
errorto the field identified bykey.inherited -
setFieldErrorsBulk(
E key, List< String> errors) → void -
Adds multiple
errorsto the field identified bykey.inherited -
setTimedErrorToField(
BlocxFormEventSetTimedErrorToField< E> event, Emitter<BlocxFormState< emit) → Future<F, E> >void> -
Handles BlocxFormEventSetTimedErrorToField.
inherited
-
submit(
BlocxFormEventSubmit event, Emitter< BlocxFormState< emit) → Future<F, E> >void> -
Handles form submission.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
-
updateData(
BlocxFormEventUpdateData< E> event, Emitter<BlocxFormState< emit) → FutureOr<F, E> >void> -
Handles a single field update event.
inherited
-
updateFormData(
E key, dynamic data) → Future< F> -
Updates the form data by field key and runs validation.
inherited
-
validateForm(
F formData, {E? key, bool forceFullValidation = false}) → FutureOr< void> -
Validates the form.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited