Как конвертировать assetImage в Uint8List/Файл

#flutter

Вопрос:

Я пытаюсь преобразовать assetImage со ссылкой («активы/изображение.png») в файл/Uint8List/

 final ByteData byteData = await rootBundle.load(logoUrl);
final File assetLogoFile =
File("${(await getTemporaryDirectory()).path}/$logoUrl");

File markerLogoFile = await assetLogoFile.writeAsBytes(byteData.buffer
        .asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

final Uint8List logoUint8List = await markerLogoFile.readAsBytes();
 

Я получаю следующую ошибку;

 E/flutter ( 5136): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.example.project/cache/assets/images/flare.png' (OS Error: No such file or directory, errno = 2)
E/flutter ( 5136): #0      _File.open.<anonymous closure> (dart:io/file_impl.dart:356:9)
E/flutter ( 5136): #1      _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter ( 5136): #2      _CustomZone.runUnary (dart:async/zone.dart:1265:19)
 

Ответ №1:

Вы можете использовать следующий код для преобразования файла активов в Uint8List;

  final ByteData bytes = await rootBundle.load('assets/image.jpg');
 final Uint8List list = bytes.buffer.asUint8List();