mapItems<U extends FieldWidgets<T>> method

List<Map<String, dynamic>> mapItems<U extends FieldWidgets<T>>(
  1. String key,
  2. List<DataFieldItem>? items,
  3. U create(
    1. DataFieldItem dataItem
    ), [
  4. U? itemsObj,
])

Implementation

List<Map<String, dynamic>> mapItems<U extends FieldWidgets<T>>(String key,
    List<DataFieldItem>? items, U Function(DataFieldItem dataItem) create,
    [U? itemsObj]) {
  //
  items = [];

  itemsObj ??= this as U?;

  itemsObj!.items ??= <U>[];

  // A new value must be added to the 'items' iterable.
  if (itemsObj.value is String) {
    final String value = itemsObj.value;
    if (value.isNotEmpty) {
      final newItem = create(DataFieldItem(
        label: itemsObj.label,
        value: itemsObj.value,
        type: itemsObj.type,
      ));
      itemsObj.items = itemsObj.items!.toList()..add(newItem);
    }
    // Clear them just to be safe.
    itemsObj.value = '';
    itemsObj.type = '';
  }

  final list = <Map<String, dynamic>>[];

  //ignore: unnecessary_cast
  for (final item in (itemsObj.items ?? []) as Iterable) {
    // Assign the appropriate map key value.
    item.keys(value: key);
    list.add(item.toMap);
  }

  return list;
}