I have a Flutter function from a package that returns a Stream<List<int>>
and I want to cast/transform to a Stream<Uint8List>
. I'm not very familiar with dart Streams so I will appreciate any help/suggestion on this topic.
I have tried to cast()
, as Stream<Uint8List>
, but did not work at all.
It's very likely that you have a Stream<List<int>>
that already has Uint8List
elements. If so, you should be able to cast the elements (not the Stream
itself) by using Stream.cast
:
var uint8ListStream = originalStream.cast<Uint8List>();
If that doesn't work, then you can create a new Stream
from the original with Stream.map
, copying each List<int>
into a new Uint8List
:
var uint8ListStream = originalStream.map((list) => Uint8List.from(list));
You also could use asUint8List
(from the answer I linked to above) to combine the approaches, copying into new Uint8List
s only if necessary:
var uint8ListStream = originalStream.map((list) => list.asUint8List());