GiancarloCode/form_bloc

Cannot submit in List and Group Fields in form_bloc_web when everything is empty

aideric opened this issue · 1 comments

Step to reproduce:

  1. Press the List and Group Fields in form_bloc_web.
  2. Press the submit button.

@BreX900

This bug occur due to

static bool areFieldBlocsValidating(Iterable<FieldBloc> fieldBlocs) =>
fieldBlocs.every(_isFieldBlocValidating);

When the list is empty, the list.every function return true. Since the members value in ListFieldFormBloc is empty in initial, the areFieldBlocsValidating return true, the field's isValidating = true. So ListFieldFormBloc cannot be submitted.
This can be fix by change it to the following

  static bool areFieldBlocsValidating(Iterable<FieldBloc> fieldBlocs) =>
      fieldBlocs.isEmpty ? false : fieldBlocs.every(_isFieldBlocValidating);

Based on OP's suggestion, I suggest a temporary fix for current users by creating a new ListFieldBloc subclass with the following implementation:

import 'package:flutter_form_bloc/flutter_form_bloc.dart' as ffb;

class _ListFieldBloc<T extends ffb.FieldBloc, ExtraData> extends ffb.ListFieldBloc<T, ExtraData> {
  _ListFieldBloc({super.name, super.fieldBlocs = const [], super.extraData});

  @override
  ffb.ListFieldBlocState<T, ExtraData> get state {
    final ffb.ListFieldBlocState<T, ExtraData> state = super.state;
    final List<T> fieldBlocs = state.fieldBlocs;
    return state.copyWith(
      isValid: fieldBlocs.isEmpty
          ? false
          : ffb.MultiFieldBloc.areFieldBlocsValid(fieldBlocs),
      isValidating: fieldBlocs.isEmpty
          ? false
          : ffb.MultiFieldBloc.areFieldBlocsValidating(fieldBlocs),
    );
  }
}

You can use now _ListFieldBloc instead of the original ListFieldBloc while an official fix is not released.
It works for me, but I don't know if it works for all use cases.