toEncodableJSONList function

List toEncodableJSONList(
  1. List? list
)

Ensures that list is an encodable JSON tree.

Implementation

List toEncodableJSONList(List? list) {
  if (list == null) return [];

  if (list is List<num>) return list;
  if (list is List<int>) return list;
  if (list is List<double>) return list;
  if (list is List<bool>) return list;
  if (list is List<String>) return list;

  if (list is List<List<num>>) return list;
  if (list is List<List<int>>) return list;
  if (list is List<List<double>>) return list;
  if (list is List<List<bool>>) return list;
  if (list is List<List<String>>) return list;

  if (list is List<Map<String, num>>) return list;
  if (list is List<Map<String, int>>) return list;
  if (list is List<Map<String, double>>) return list;
  if (list is List<Map<String, bool>>) return list;
  if (list is List<Map<String, String>>) return list;

  if (list.isEmpty) return <dynamic>[];

  if (isListEntriesAllOfType(list, num)) return List<num>.from(list);
  if (isListEntriesAllOfType(list, int)) return List<int>.from(list);
  if (isListEntriesAllOfType(list, double)) return List<double>.from(list);
  if (isListEntriesAllOfType(list, bool)) return List<bool>.from(list);
  if (isListEntriesAllOfType(list, String)) return List<String>.from(list);

  return list.map((e) => toEncodableJSON(e)).toList();
}