getImageFromGallery static method

Future<List<Uint8ListWithName>> getImageFromGallery({
  1. bool allowMultiple = false,
})

Implementation

static Future<List<Uint8ListWithName>> getImageFromGallery({
  bool allowMultiple = false,
}) async {
  FilePickerResult? result = await FilePicker.pickFiles(
    type: FileType.media,
    allowMultiple: allowMultiple,
    withData: true,
  );
  if (result == null) return [];
  if (result.files.length == 1) {
    File imageFile = File(result.files.single.path!);
    Uint8List? image = await editImage(imageFile);
    return image != null
        ? [Uint8ListWithName(image: image, name: result.files.single.name)]
        : [];
  }
  return Future.wait(
    result.files
        .where((file) => file.path != null)
        .map(
          (file) async => Uint8ListWithName(
            image: await File(file.path!).readAsBytes(),
            name: file.name,
          ),
        ),
  );
}