removeElementWithKey method

void removeElementWithKey(
  1. dynamic key
)

Remove element under key

Implementation

void removeElementWithKey(dynamic key) {
  if (_rawValue is List) {
    List rawList = _rawValue as List;
    late int index;

    if (key is String) {
      try {
        index = int.parse(key);
      } catch (_) {
        throw JsonException(
          JsonError.wrongType,
          userReason: 'JSON Error: index must be int, ${key.runtimeType} given',
        );
      }
    } else {
      index = key as int;
    }

    rawList.removeAt(index);
  } else if (_rawValue is Map) {
    Map rawMap = _rawValue as Map;
    late String index;

    if (key is String) {
      index = key;
    } else {
      index = '$key';
    }

    rawMap.remove(index);
  } else {
    throw throw JsonException(
      JsonError.wrongType,
      userReason: '_rawValue is not a List or a Map',
    );
  }
}