Add to list from dart: typed_data
Dart typed_data library contains several lists (eg Uint8List
, Uint16List
etc.) that are more effective than traditional List
, and is especially useful when you need to work with binary data. However, these lists are all fixed-sized lists, so it is not possible to extend (or add to the list) or shrink the list once it is created. This limitation causes a real problem when the size of the resulting list is unknown at the time of creation.
Consider the following example:
Uint8List getEven(Uint8List in)
{
Uint8List out = new Uint8List(0);
for (num i = 0; i < in.length; ++i)
{
if (in[i] % 2 == 0)
out.add(in[i]); // <- This will throw an exception since Uint8List is fixed-length
}
return out;
}
Is there any workaround for this problem or do I need to use List<num>
to handle binary data if I need add / resize functionality?
source to share
Yes, lists are dart:typed_data
not available in the built-in library .
However, the package contains an additional typed_data
package that provides mutable typed data implementations . I think I haven't tried them yet, but I hope they (still) work.
Uint8Buffer
will be equivalent to the list you are looking for.
source to share