flutter_image_compress
flutter_image_compress copied to clipboard
compressWithList sometimes gives null error
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
I'm getting this error constantly, any update on this?
I'm getting this error constantly, too.
any updates ?