Home > database >  Flutter: How to convert AssetImage to Unit8List?
Flutter: How to convert AssetImage to Unit8List?

Time:08-30

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()

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

CodePudding user response:

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();
  • Related