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
Asked
Active
Viewed 3.6k times
3 Answers
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
-
1Isn't there problem with UTF-16 characters? – AgainPsychoX May 29 '19 at 04:35
-
I don't think so, but it's possible. Should be easy to try. – Günter Zöchbauer May 29 '19 at 04:37
-
2Yes, there is a problem (online code [confirmation](https://dartpad.dartlang.org/f2b915b32ad42e96f04eb11b5d916a78)). I will try my best to come up with updated answer. – AgainPsychoX May 29 '19 at 04:48
-
Hi @PsychoX, any link to UTF-16 working version ? Thanks. – JerryZhou Aug 17 '19 at 00:21
-
1@GünterZöchbauer I test with `x√xx` and `x√x周x`,output is `xxx` and `xxhx`, here's the [gist link](https://gist.github.com/57bead40f336151113373fce37247b17) – JerryZhou Aug 17 '19 at 00:37
-
Find a more generic answer: https://stackoverflow.com/a/54844226/1530581 – JerryZhou Aug 17 '19 at 01:17
-
There is universal solutions: https://stackoverflow.com/questions/28565242/convert-uint8list-to-string-with-dart/56363052#56363052 – AgainPsychoX Aug 17 '19 at 16:23
-
1Warning: Uint8List.fromList(list) will truncate your integers to 8-bit integers. – Martin Berger Dec 02 '22 at 13:01
-
THIS ANSWER IS WRONG. Use utf8 encoding or face the consequences. – Rexios Jul 11 '23 at 13:35
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