createJson method

String createJson(
  1. File file,
  2. Map<String, dynamic>? dict,
  3. Map<String, dynamic> report
)

Implementation

String createJson(
    File file, Map<String, dynamic>? dict, Map<String, dynamic> report) {
  Map<String, dynamic> content = {};
  List newKeys = [];
  List delKeys = [];
  if (file.existsSync()) {
    try {
      content = jsonDecode(file.readAsStringSync()) as Map<String, dynamic>;
      // ignore: empty_catches
    } catch (err) {}
  }
  List<String> lines = [];
  for (var key in dict!.keys.toList()..sort()) {
    if (!content.containsKey(key)) {
      newKeys.add(key);
    }
    lines.add('\t"$key": ${jsonEncode(content[key] ?? dict[key])}');
    content.remove(key);
  }
  for (var key in content.keys) {
    delKeys.add('$key = ${content[key]}');
  }
  if (newKeys.isNotEmpty) {
    report['new_keys_file'].add([file.path, newKeys, newKeys.length]);
  }
  if (delKeys.isNotEmpty) {
    report['del_keys_file'].add([file.path, delKeys, delKeys.length]);
  }
  String json = '{\n${lines.join(',\n')}\n}';
  file.writeAsString(json);
  return json;
}