fluttercandies / flutter_image_compress

flutter image compress
MIT License
633 stars 214 forks source link

compressWithList sometimes gives null error #212

Open atillaturkmen opened 2 years ago

atillaturkmen commented 2 years ago

compressWithList function sometimes gives the following error:

Unhandled Exception: type 'Null' is not a subtype of type 'FutureOr<Uint8List>'

I have tried calling this function with the same bytes that are read from the same picture selected with image picker library in a loop. The result is the function sometimes compresses the picture successfully and sometimes gives the error above.

Relevant code is below:

final ImagePicker _picker = ImagePicker();

void _selectImage() async {
  // select image from gallery
  XFile? image = await _picker.pickImage(
    source: ImageSource.gallery,
  );
  print(image);
  if (image != null) {
    // compress the image
    Uint8List imageBytes = await File(image.path).readAsBytes();
    print(imageBytes.length);
    Uint8List compressed = await compressChatImage(imageBytes);
    print(compressed.length);
  }
}

const PIC_QUALITY = 25;
const PIC_WIDTH = 400;
const PIC_HEIGHT = 400;

Future<Uint8List> compressChatImage(Uint8List bytes) async {
  while (true) {
    try {
      return await FlutterImageCompress.compressWithList(
        bytes,
        quality: PIC_QUALITY,
        format: CompressFormat.png,
        minWidth: PIC_WIDTH,
        minHeight: PIC_HEIGHT,
      );
    } catch (err) {
      print(err);
    }
  }
}

Output:

Instance of 'XFile'
4322440
type 'Null' is not a subtype of type 'FutureOr<Uint8List>'
type 'Null' is not a subtype of type 'FutureOr<Uint8List>'
type 'Null' is not a subtype of type 'FutureOr<Uint8List>'
type 'Null' is not a subtype of type 'FutureOr<Uint8List>'
346085
tomislavm-9 commented 1 year ago

I'm getting this error constantly, any update on this?

shinpeiyamagiwa commented 1 year ago

I'm getting this error constantly, too.

iak-a-dev commented 4 months ago

any updates ?