parseUInt8List static method

Uint8List? parseUInt8List(
  1. Object? value, [
  2. Uint8List? def
])

Tries to parse a Uint8List.

  • Returns def if value is invalid.

Implementation

static Uint8List? parseUInt8List(Object? value, [Uint8List? def]) {
  if (value == null) return def;

  if (value is Uint8List) {
    return value;
  } else if (value is List<int>) {
    return Uint8List.fromList(value);
  } else if (value is Iterable<int>) {
    return Uint8List.fromList(value.toList(growable: false));
  } else if (value is Iterable) {
    var list = value.map((e) => parseInt(e)).toList(growable: false);
    if (list.any((e) => e == null)) {
      return def;
    }
    return Uint8List.fromList(list.cast<int>());
  } else {
    var s = value.toString().trim();

    if (s.isEmpty) return Uint8List(0);

    try {
      var data = base64.decode(s);
      return data;
    } catch (_) {}

    try {
      var data = base_codecs.hex.decode(s);
      return data;
    } catch (_) {}

    return def;
  }
}