flutter_image_compress icon indicating copy to clipboard operation
flutter_image_compress copied to clipboard

compressWithList sometimes gives null error

Open atillaturkmen opened this issue 2 years ago • 4 comments

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

atillaturkmen avatar Jun 13 '22 08:06 atillaturkmen

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

tomislavm-9 avatar May 30 '23 20:05 tomislavm-9

I'm getting this error constantly, too.

shinpeiyamagiwa avatar Sep 06 '23 09:09 shinpeiyamagiwa

any updates ?

iak-a-dev avatar Apr 29 '24 11:04 iak-a-dev