saveCollection<T> static method

Future saveCollection<T>(
  1. String key,
  2. List collection
)

Save a list of objects to a collection using a key.

Implementation

static Future saveCollection<T>(String key, List collection) async {
  if (["dynamic", "string", "double", "int"]
      .contains(T.toString().toLowerCase())) {
    await save(key, jsonEncode(collection));
    return;
  }

  String json = jsonEncode(collection.map((item) {
    Map<String, dynamic>? data = _objectToJson(item);
    if (data != null) {
      return data;
    }
    return item;
  }).toList());
  await save(key, json);
}