valueAsBytes<T extends List<int>?> static method

T valueAsBytes<T extends List<int>?>(
  1. Object? value, {
  2. bool allowHex = true,
  3. StringEncoding? encoding,
})

Implementation

static T valueAsBytes<T extends List<int>?>(Object? value,
    {bool allowHex = true, StringEncoding? encoding}) {
  if (value == null && _isNull<T>()) return null as T;
  if (value is List) {
    try {
      return value.cast<int>().asBytes as T;
    } catch (_) {
      throw JSONHelperException("Failed to parse value as List of int.");
    }
  }

  if (value is String) {
    if (allowHex) {
      final toBytes = BytesUtils.tryFromHexString(value);
      if (toBytes != null) return toBytes as T;
    }
    if (encoding != null) {
      final toBytes = StringUtils.tryEncode(value, type: encoding);

      if (toBytes != null) return toBytes as T;
      throw JSONHelperException("Failed to parse value as List of int.");
    }
  }

  throw JSONHelperException("Failed to parse value as List of int.");
}