Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter: How to convert AssetImage to Unit8List?

I am trying to insert to a SQFlite database a webp image that I have in my assets.
But I don’t know how to convert the asset image to a Uint8List which is the data type in my DB.
How can I do it?

I have tried this:

Future<Uint8List> convert() async {
 final ByteData bytes = await rootBundle.load('assets/ab.webp');
 final Uint8List list = bytes.buffer.asUint8List();
 return list;
}

Uint8List list = convert();

But I get the following error:
Type: Future Function()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A value of type ‘Future’ can’t be assigned to a variable of type ‘Uint8List’.
Try changing the type of the variable, or casting the right-hand type to ‘Uint8List’.

Thank you in advance

>Solution :

convert() is a async function so when you want to use it you should await for the result and also your convert function does not rerturn any thing.

try this:

Future<Uint8List> convert() async {
 final ByteData bytes = await rootBundle.load('assets/image.webp');
 final Uint8List list = bytes.buffer.asUint8List();
 return list;
}

then use it like this:

Uint8List list = await convert();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading