writeDataToJson method

void writeDataToJson(
  1. Map<String, PretDataclass> data,
  2. String fieldName
)

Persist new data to a top-level field in the JSON data file, given data and field name. Throws a ArgumentError if the field name is not found in the JSON file.

This function is the recommended method for saving new or updated data to the JSON file, as it also takes care of updating the schema version and dropping unneeded fields.

Implementation

void writeDataToJson(Map<String, PretDataclass> data, String fieldName) {
  final Map rawData = pretLoadJson();
  if (!rawData.containsKey(fieldName)) {
    throw ArgumentError.value(fieldName);
  }
  rawData[fieldName] = data.map(
    (key, value) => MapEntry(key, value.toJson()),
  );
  rawData['schema'] = schemaVersion;
  rawData.removeWhere(
      (key, value) => dropFields[schemaVersion]?.contains(key) ?? false);
  final stringifiedData = encoder.convert(rawData);
  _putJson(stringifiedData);
}