setJsonValue method

  1. @override
void setJsonValue(
  1. String file,
  2. List<Object> path,
  3. Object? value
)
override

Writes into a nested JSON object / list while preserving all sibling keys; missing containers are created (a list when the next path element is an int).

Implementation

@override
void setJsonValue(String file, List<Object> path, Object? value) {
  final plat = current;
  if (plat == null || path.isEmpty) return;
  dynamic node = plat.file(file);
  for (var i = 0; i < path.length - 1; i++) {
    final key = path[i];
    final nextKey = path[i + 1];
    if (node is Map) {
      final next = node[key];
      node = next is Map || next is List ? next : (node[key] = nextKey is int ? <dynamic>[] : <String, dynamic>{});
    } else if (node is List && key is int) {
      while (node.length <= key) {
        node.add(null);
      }
      final next = node[key];
      node = next is Map || next is List ? next : (node[key] = nextKey is int ? <dynamic>[] : <String, dynamic>{});
    } else {
      return;
    }
  }
  final last = path.last;
  if (node is Map) {
    node[last] = value;
  } else if (node is List && last is int) {
    while (node.length <= last) {
      node.add(null);
    }
    node[last] = value;
  }
  configEdited(file);
}