subscribeToFieldBlocs method

void subscribeToFieldBlocs(
  1. List<FieldBloc<FieldBlocStateBase>> fieldBlocs
)

Create a subscription to the state of each fieldBloc in FieldBlocs, When any state changes, this fieldBloc will be revalidated. This is useful when you have validators that uses the state of other fieldBloc, for example when you want the correct behavior of validator that confirms a password with the password of other fieldBloc.

Implementation

void subscribeToFieldBlocs(List<FieldBloc> fieldBlocs) {
  _revalidateFieldBlocsSubscription?.cancel();
  // TODO: When async validation is in progress and auto validate is off this method is broken
  // because it emit a completed async validation
  // TODO: It does not manage MultiFieldBloc fields
  if (fieldBlocs.isNotEmpty) {
    _revalidateFieldBlocsSubscription = Rx.combineLatest<dynamic, void>(
      fieldBlocs.whereType<SingleFieldBloc>().toList().map(
        (state) {
          return state.stream.map<dynamic>((state) => state.value).distinct();
        },
      ),
      (_) {},
    ).listen((_) {
      if (_autoValidate) {
        _validate();
      } else {
        emit(state.copyWith(
          isValidated: false,
          isValidating: false,
        ) as State);
      }
    });

    if (_autoValidate) {
      _validate();
    } else {
      emit(state.copyWith(
        isValidated: false,
        isValidating: false,
      ) as State);
    }
  }
}