Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: 'Lost connection to device.' second time using image_picker to select photo from gallery on iOS

I am developing a mobile app in flutter that requires the user to select an image from the gallery.

I am using image_picker: ^0.6.7+11 and here is my code:

if (await Permission.photos.request().isGranted) {
    try {
      final image =
          await ImagePicker().getImage(source: ImageSource.gallery);
      if (image != null) {
        photo.clearData();
        File _image = File(image.path);
        photo.addOriginal(_image);
      } else {
        print('no image selected');
      }
    } on PlatformException catch (e) {
      print('Platform exception $e');
    } catch (e) {
      print('Unknown error: $e');
    }
  }

In the android emulator, everything works fine. In the iOS simulator I am able to choose an image, but if I try to choose a second image then the app crashes and 'Lost connection to device.' is printed in the run tab - but no errors.

Question: How can I fix this so that I can go back to the gallery and select a different image as many times as I want on iOS?

When debugging I have come across this:

PlatformException(multiple_request, Cancelled by a second request, null, null)

I have gone through as many similar questions as I can find on here, GitHub etc... I have:

  • upgraded flutter
  • upgraded pub files
  • flutter clean
  • clean and build in Xcode
  • added 'imageCache.clear()' in flutter code
  • invalidate caches and restart in android studio
  • flutter doctor -v : no issues found
  • checked the permissions (info.plist and added permission_handler package)
  • restarted the simulator
  • erased all contents and settings in simulator
  • debugging with breakpoints weirdly stops the problem from happening a few times then after a few selections the app crashes again ¯\(ツ)

I'm sure its something straight forward but I feel like I have exhausted all my options and not sure where to go from here.

like image 580
goh Avatar asked Oct 05 '20 11:10

goh


People also ask

How do you access your gallery from flutter?

Flutter comes with an image picker plugin for picking images from the device gallery or taking new pictures from the camera. The image_picker plugin exposes some helpful methods from the ImagePicker class it exports: import 'package:image_picker/image_picker. dart'; ImagePicker picker = ImagePicker();


1 Answers

Your code is right and it is a simulator problem,if you want to test it on IOS, you have to test on a real IPhone and here is a snippet on how properly you can do it:

final imagePicker = ImagePicker();
    PickedFile pickedFile;
    if (fileType == FileType.Camera) {
      // Camera Part
      pickedFile = await imagePicker.getImage(
        source: ImageSource.camera,
        maxWidth: 480,
        maxHeight: 640,
        imageQuality: 25, // pick your desired quality
      );
      setState(() {
        if (pickedFile != null) {
          _storedFile = File(pickedFile.path);
        } else {
          print('No image selected.');
          return;
        }
      });
    } else if (fileType == FileType.Gallery) {
      // Gallery Part
      pickedFile = await imagePicker.getImage(
        source: ImageSource.gallery,
        maxWidth: 480,
        maxHeight: 640,
        imageQuality: 25,
      );
        } else {
          print('No image selected.');
          return;
        }
      });
    }

and don't forget the enum when the user picks the way he wants to get the image:

enum FileType {
  Gallery,
  Camera,
  Video,
}

Edit: whenever you add a package that depends on native code, you should restart the whole app build b stopping your build and restarting it again so the native code compiles well, don't forget to restart before putting this package in your pubspec.yaml

Hope it helps, if this answer helped you, don't forget to mark as answered and upvote it please.

like image 162
Ziyad Mansy Avatar answered Oct 20 '22 13:10

Ziyad Mansy