computeData method

Map<String, dynamic> computeData([
  1. Map<String, dynamic>? data
])

Implementation

Map<String, dynamic> computeData([Map<String, dynamic>? data]) {
  if (files.isNotEmpty) {
    // Picks the first file.
    final file = files.first;
    setData({
      "svgProp": {
        "type": "image/svg+xml",
        "size": file.size.toString(),
        "data": base64.encode(file.bytes ?? []),
        "lastModified": DateTime.now().toUtc().toIso8601String(),
        "name": file.name,
      }
    });
  }

  // get the data to work on
  final actualData = data ?? textEditingControllerMapping;

  return actualData.map((key, value) {
    if (value is TextEditingController) {
      // If the value is empty, we should nullify it
      return value.text.isNotEmpty
          ? MapEntry(key, value.text)
          : MapEntry(key, null);
    } else if (value is Map<String, dynamic>) {
      // If the value is a map, we should recursively call this function
      return MapEntry(key, computeData(value));
    }
    // Ideally, we should never reach this point
    return MapEntry(key, "isso não deve acontecer");
  })
    // Where the value is null, we remove it
    ..removeWhere((key, value) => value == null)
    // Where the the value is a map, we remove it if it is empty
    ..removeWhere(
      (key, value) => value is Map ? value.isEmpty : false,
    );
}