I'm building a bunch of Uint8List
(of different sizes, for now they are stored in a generic List) and I need to combine/concatenate them before sending on a websocket.
What would be the best approach ?
I though of combining them in a new Uint8List, but since I don't need byte access anymore after it is combined, I can maybe use a different List<int>
implementation ... ?
Thanks in advance.
Uint8List
implements List<int>
. You can combine them to a new List<int>
and then create a new Uint8List
with
List<List<int>> myByteLists = ...;
var bytes = Uint8List.fromList(myByteList.expand((x) => x).toList());
Using BytesBuilder seems to be the most efficient way to concatenate Uint8List's in Dart:
var b = BytesBuilder();
var l1 = Uint8List(4);
var l2 = Uint8List(4);
b.add(l1);
b.add(l2);
var ll = b.toBytes();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With