skip to Main Content

I want to deselect images if I selected more than 4 images, here i can’t go back gallery

List<XFile?> imageFiles =  await ImagePicker.platform.pickMultiImage();

 if(imageFiles.length <= 4) {
   ///successfully uploaded images
 } else{
   ///go back to gallery to deselect images until <= 4
 }

I want to go back to gallery to deselect selected images

2

Answers


  1. Try once the below code :-

    final ImagePicker _picker = ImagePicker();
    List<XFile>? _selectedImages = [];
    
    Future<void> _pickImages() async {
    try {
      final List<XFile>? pickedImages = await _picker.pickMultiImage(
        imageQuality: 50,
      );
    
      if (pickedImages != null && pickedImages.isNotEmpty) {
        setState(() {
          _selectedImages = pickedImages.take(4).toList();
        });
      }
    } catch (e) {
      print('Error picking images: $e');
    }
    
    Login or Signup to reply.
  2. You can use library multi_image_picker_view to limit the number of images selected instead of coding the logic yourself.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search