Home > Enterprise >  The await expression can only be used in an async function
The await expression can only be used in an async function

Time:12-16

I create function in flutter to convert images assets to file and when I call this function with await, the error tell me to add async in the body of my function :

Future<File> getImageFileFromAssets(String path) async {
  final byteData = await rootBundle.load('assets/$path');

  final file = File('${(await getTemporaryDirectory()).path}/$path');
  await file.writeAsBytes(byteData.buffer
      .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

  return file;
}

the call :

File ppBase = await getImageFileFromAssets('ppBase.png'); 

Can you help me please

I don't know what is the problem

CodePudding user response:

You need to convert your method to async to use await

yourMethod() async { //here async
   File ppBase = await getImageFileFromAssets('ppBase.png'); 
  • Related