Home > Software design >  Dart Flutter Uint8List
Dart Flutter Uint8List

Time:05-26

Why does calling .reversed on an instance of Uint8List not return an instance of Uint8List in reversed byte order. Is there some logical reason for not returning a Uint8List in reversed byte order ?

Uint8List uint8List = Uint8List(20); //20 byte array

Uint8List reversed =  uint8List.reversed; //error not returning Uint8List

CodePudding user response:

Because the reversed property is inherited from the Uint8List's superclass List (technically from Iterable, which is List's superclass).

Since reversed is part of Iterable, it returns an Iterable and not an Uint8List. You can, of course, simply convert it to a List.

Uint8List reverseUint8List(Uint8List list) {
  return Uint8List.fromList(list.reversed.toList());
}

Long story short, it's to prevent having to duplicate 99% of the reversed code for each implementation of Iterable, and while it does not make sense for the output (a reversed list cannot suddenly become a queue/set/whatever), it does make sense implementation wise, since these data types are all similar in that regard.

CodePudding user response:

You have to use this:

uint8List.reversed.toList()

as reversed returns an Iterable.

  • Related