getImageFileFromAssets static method

Future<File> getImageFileFromAssets(
  1. String path
)

getImageFileFromAssets method to get image file from assets This method takes image path as input This method returns the image file This method uses rootBundle to load the image This method uses getTemporaryDirectory to get the temporary directory This method uses File class to create the file This method uses writeAsBytes method to write the image This method is an async method This method is a static method

Example:

ImageUtils.getImageFileFromAssets("assets/image.png");

Implementation

static Future<File> getImageFileFromAssets(String path) async {
  // globalLogger.d('getImageFileFromAssets: $path', error: 'ImageUtils');
  final temp = await getTemporaryDirectory();
  // globalLogger.d('getImageFileFromAssets temp: ${temp.path}', error: 'ImageUtils');
  final file = File('${temp.path}/$path');
  // globalLogger.d('getImageFileFromAssets file: ${file.path}', error: 'ImageUtils');
  await file.create(recursive: true);
  // try {
  final byteData = await rootBundle.load(path);
  await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
  // } catch (e) {
  //   globalLogger.e('getImageFileFromAssets: $e', error: 'ImageUtils');
  // }

  return file;
}