safeByteArray method

Future<Uint8List> safeByteArray(
  1. BuildContext context
)

A future that retrieves the image data as a Uint8List from the appropriate source based on the EditorImageType.

Implementation

Future<Uint8List> safeByteArray(BuildContext context) async {
  Uint8List bytes;
  switch (type) {
    case EditorImageType.memory:
      return byteArray!;
    case EditorImageType.asset:
      bytes = await loadAssetImageAsUint8List(assetPath!);
      break;
    case EditorImageType.file:
      bytes = await readFileAsUint8List(file!);
      break;
    case EditorImageType.network:
      bytes = await fetchImageAsUint8List(networkUrl!);
      break;
  }

  if (!context.mounted) return bytes;

  await precacheImage(
    MemoryImage(bytes),
    context,
    size: MediaQuery.of(context).size,
  );

  byteArray = bytes;

  return bytes;
}