I need to convert a Future<Uint8List>
to Base64 or PNG in Flutter, I am using this pub to get signature and export it but when I call toPngBytes()
method (method in pub) it returns a Future<Uint8List>
and I need to convert it to Base64
format or List<int>
at least ByteData
format, I can not convert it to a more usable format for me, can anyone help me to resolve this.
_controller.toPngBytes(); // _controller is a variable that holds info about my signature.
import 'dart:convert';
// async variant
final imageData = await _controller.toPngBytes(); // must be called in async method
final imageEncoded = base64.encode(imageData); // returns base64 string
// callback variant
_controller.toPngBytes().then((data) {
final imageEncoded = base64.encode(data);
});
This function made me upload an image to Xano correctly:
import 'dart:convert';
import 'dart:typed_data';
String uint8ListTob64(Uint8List uint8list) {
String base64String = base64Encode(uint8list);
String header = "data:image/png;base64,";
return header + base64String;
}
You can transform an image into Uint8List in the following way:
String path = "image.png";
File file = File(path);
Uint8List uint8list = file.readAsBytesSync();
//or
Uint8List uint8list = await file.readAsBytes();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.