31

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

Osama Gamal
  • 2,280
  • 5
  • 17
  • 28

3 Answers3

63

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);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
14

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);
Faruk
  • 5,438
  • 3
  • 30
  • 46
4

the best and simple way of doing it is,

import 'dart:convert';

Image.memory(base64Decode(string_value_here));
Community
  • 1
  • 1
Akhlaq Shah
  • 340
  • 2
  • 8