getMapFromJson method

dynamic getMapFromJson(
  1. String? path, {
  2. required void callbackJsonIsList(),
  3. String warningMessage = 'Format json not valid!',
})

Implementation

dynamic getMapFromJson(
  String? path, {
  required void Function() callbackJsonIsList,
  String warningMessage = 'Format json not valid!',
}) {
  final json = path != null ? File(path).readAsStringSync() : '{}';
  dynamic response;
  try {
    response = jsonDecode(json);
    if (response is List) {
      callbackJsonIsList();
      response = response.first;
    }
  } catch (e) {
    StatusHelper.warning(warningMessage);
    return null;
  }

  if (response is! Map) {
    StatusHelper.warning(warningMessage);
    return null;
  }
  return response;
}