asMapOr method

JsonMap? asMapOr([
  1. Map<String, dynamic>? defaultValue
])

Convert Json to the JsonMap or create JsonMap from defaultValue

Implementation

JsonMap? asMapOr([Map<String, dynamic>? defaultValue]) {
  final json = _jsonObject;
  if (json is Map<String, dynamic>) {
    return JsonMap(json, _onError);
  } else if (json is JsonMap) {
    return json;
  } else if (json is String) {
    final parsedJson = _parseJsonObjectString(json);
    if (parsedJson != null) {
      return parsedJson.asMapOr(defaultValue);
    }
  }
  if (json != null && json is! String) {
    (_onError ?? onError).call(_jsonHasUnsupportedType(json.runtimeType.toString()));
  }
  return defaultValue != null ? JsonMap(defaultValue, _onError) : null;
}