Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert a String of "uint8list" to a Unit8List

Tags:

flutter

dart

I have a memory image stored in Sqllite converted to String with the toString() method, I want to convert it to Unit8List to display it inside a MemoryImage widget

like image 344
Osama Gamal Avatar asked Jul 31 '18 12:07

Osama Gamal


People also ask

What is Uint8List flutter?

Uint8List class Null safety. A fixed-length list of 8-bit unsigned integers. For long lists, this implementation can be considerably more space- and time-efficient than the default List implementation.

How do I use image memory in flutter?

Using MemoryImage Constructor It contains the bytes to be decoded into an image. The other, which is optional, is a double named scale , used the scale the size of the image. Therefore, you need to have the image data as Unit8List bytes. To convert an image into Unit8List , you can make use of Flutter's AssetBundle .


2 Answers

codeUnits gets you a List<int>
Uint8List.fromList(...) converts List<int> to Uint8List
String.fromCharCodes(...) converts List<int> or Uint8List to String

List<int> list = 'xxx'.codeUnits;
Uint8List bytes = Uint8List.fromList(list);
String string = String.fromCharCodes(bytes);
like image 84
Günter Zöchbauer Avatar answered Sep 23 '22 02:09

Günter Zöchbauer


Use utf8.encode(myString) to convert String to bytes or List<int>,

And then convert it back using utf8.decode(bytes)

String source = 'Błonie';

List<int> list = utf8.encode(source);
Uint8List bytes = Uint8List.fromList(list);
String outcome = utf8.decode(bytes);
like image 24
faruk Avatar answered Sep 22 '22 02:09

faruk