castBytes function Null safety

Uint8List castBytes(
  1. List<int> bytes,
  2. {bool copy = false}
)

Cast the list of bytes into a typed Uint8List.

When copy is specified, the content will be copied even if the input bytes are already Uint8List.

Implementation

Uint8List castBytes(List<int> bytes, {bool copy = false}) {
  if (bytes is Uint8List) {
    if (copy) {
      final list = Uint8List(bytes.length);
      list.setRange(0, list.length, bytes);
      return list;
    } else {
      return bytes;
    }
  } else {
    return Uint8List.fromList(bytes);
  }
}