14

I am using a riverpod provider class to handle picking of image from gallery. However, once an image is picked, I get the error: PlatformException(multiple_request, Cancelled by a second request null, null). Not sure where a second request is coming from. More importantly, no image is applied to my placeholder (CircleAvartar) due to this unknown cancellation. Here are the two dart files in question and thanks for the help.

imageProvider file:

final myImageProvider =
    ChangeNotifierProvider<ImageNotifier>((ref) => ImageNotifier());

class ImageNotifier extends ChangeNotifier {
  ImageNotifier() : super();
  final file = useState<File?>(null);
  final imageFile = useState<XFile?>(null);
  final imagePicker = ImagePicker();

  Future<void> _pickImage(int type) async {
    try {
      XFile? userImage = await imagePicker.pickImage(
        source: type == 1 ? ImageSource.gallery : ImageSource.camera,
        imageQuality: 50,
      );
      imageFile.value = userImage;
      // imageFile.value = XFile(userImage!.path);
    } catch (e) {
      print(e);
    }
    notifyListeners();
  }

  void showPicker(context) {
    showModalBottomSheet(
      backgroundColor: Theme.of(context).primaryColor,
      context: context,
      builder: (BuildContext bc) {
        return SafeArea(
          child: Wrap(
            children: [
              ListTile(
                leading: const Icon(
                  Icons.photo_library,
                  color: Colors.white,
                ),
                title: const Text(
                  'Photo Gallery',
                  style: TextStyle(fontSize: 22),
                ),
                onTap: () => _pickImage(1),
              ),
              ListTile(
                leading: const Icon(
                  Icons.photo_camera,
                  color: Colors.white,
                ),
                title: const Text(
                  'Camera',
                  style: TextStyle(fontSize: 22),
                ),
                onTap: () => _pickImage(2),
              ),
              ListTile(
                leading: const Icon(
                  Icons.close,
                  color: Colors.white,
                ),
                title: const Text(
                  'Cancel',
                  style: TextStyle(fontSize: 22),
                ),
                onTap: () {
                  imageFile.value = null;
                  Navigator.of(context).pop();
                },
              ),
            ],
          ),
        );
      },
    );
    notifyListeners();
  }

AuthScreen dart file:

Widget build(BuildContext context, WidgetRef ref) {
    final _passwordController = useTextEditingController();
    final _passwordFocusScope = useFocusNode();
    final _emailFocusScope = useFocusNode();
    final _phoneFocusScope = useFocusNode();
    final _confirmFocusScope = useFocusNode();
    final _isVisible = useState<bool>(true);
    var _authMode = useState<AuthMode>(AuthMode.login);
    final imageProviderState = ref.watch(myImageProvider.notifier);
    final deviceSize = MediaQuery.of(context).size;
    final authMode = ModalRoute.of(context)?.settings.arguments as String;

    switch (authMode) {
      case 'login':
        _authMode.value = AuthMode.login;
        break;
      case 'register':
        _authMode.value = AuthMode.register;
        break;
      case 'google':
        _authMode.value = AuthMode.google;
        break;
      case 'guest':
        _authMode.value = AuthMode.guest;
        break;
    }

    return Scaffold(
      body: Stack(
        children: [
         
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Form(
              key: _formKey,
              child: SingleChildScrollView(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    const SizedBox(
                      height: 80,
                    ),
                    Center(
                      child: _authMode.value == AuthMode.login
                          ? const Text(
                              'Access Your Account',
                              style: TextStyle(
                                fontSize: 25,
                              ),
                            )
                          : Row(
                              children: [
                                InkWell(
                                  onTap: () =>
                                      imageProviderState.showPicker(context),
                                  // () => ref
                                  // .read(myImageProvider.notifier)
                                  // .showPicker(context),
                                  child: CircleAvatar(
                                    radius: 50,
                                    backgroundImage:
                                        imageProviderState.imageFile.value !=
                                                null
                                            ? FileImage(
                                                //   File(ref
                                                //       .read(imageProvider.notifier)
                                                //       .imageFile
                                                //       .value!
                                                //       .path),
                                                // )
                                                File(imageProviderState
                                                    .imageFile.value!.path),
                                              )
                                            : null,
                                    child: imageProviderState.imageFile.value ==
                                            null
                                        ? const Icon(
                                            Icons.camera,
                                            // Icons.add_photo_alternate,
                                            size: 30,
                                            color: Colors.white,
                                          )
                                        : null,
                                  ),
                                ),

simulator screenshot for image pick from gallery or camera

Obisi7
  • 485
  • 1
  • 5
  • 18
  • Kind community of Flutter experts, please help me with this issue of PlatformException as it concerns pickImage module. I have tried this on iOS and android (emulation and real device) but same issue. I am unable to apply the selected photo from gallery because somehow, the operation is cancelled from another request. That request is unknown to me and not from my code even though that's what is causing the issue. Thanks a million good people. – Obisi7 Feb 23 '22 at 03:22

10 Answers10

6

I have the latest Flutter 3.3.9 and Xcode 14.1 and this is still a problem. The workaround is very simple though after reading this issue. When using the image_picker, DO NOT pick the first image (with pink flowers):

avoid picking this image

Theo
  • 479
  • 5
  • 6
3

After testing the code on a real device (iPhone and Android) I was able to select and attach a photo from gallery and camera to my form. The issue is with trying to do this task on a simulator even though one was able to do so once upon a time. Don't even bother anymore until Apple fixes this trouble. My advice is that you debug on a real device to ensure things are working as coded and you can return to the simulator/emulator afterwards. I lost a lot of time trying to make tis work on a simulator to no avail.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Obisi7
  • 485
  • 1
  • 5
  • 18
2

In addition to my earlier answer and further tweaking with the dev in simulator environment, I just discovered that the uploaded image does show up upon a reload/restart. Weird but works if you must test in simulation mode. Simply restart and the uploaded image will show up. It is still a simulator issue though, IMHO.

Obisi7
  • 485
  • 1
  • 5
  • 18
1

It can help to double-click on the image you are selecting from the gallery instead of clicking only once.

For whatever reason, if I clicked only once, it would not show up and the same error as yours appeared.

If I clicked twice there was a short lag, but the image showed up.

Tested on iOS simulator - don't get this issue personally on my Android emulator.

1

I had this issue picking one of the default album images on my iOS simulator.

I was able to get around this by going to Safari, saving a png to Photos and then selecting that downloaded png in my Flutter app.

Thanks to Marc's post which pointed me in the right direction regarding HEIC support

Mans
  • 2,953
  • 2
  • 23
  • 32
0

Hi please have a look at this discussion: https://github.com/flutter/flutter/issues/70436

  • on on the image picker package site we can see that it is a well known apple simulator issue. I would say that it should work for you on real devices (or try to test it only with particular pictures from iOS simulator photos)

enter image description here

Marc Sanny
  • 816
  • 6
  • 12
  • 1
    Thanks for the reference. However, I am on iOS 15.3.1 and have tried it on real device (iPhone 13 Pro) and getting the same result: Platform Exception... multiple request. In the simulator, I was lucky to have it work only once and after that no more luck. Not sure why that's the case if the same code, unchanged, picked and displayed an image from gallery and a photo from the device, why does it stop working now? Does anything need to be disposed for image picker? Also, I am using the latest imagePicker package which requires XFile type instead of the old PickedFile type. Smart folks help me. – Obisi7 Mar 05 '22 at 01:55
  • Anyone found a solution to this debacle? It has stopped me cold in moving ahead in my development as I can't complete all the attributes needed for my product definitions. Thanks as you help – Obisi7 Mar 16 '22 at 23:46
0

Make sure ALLOW PHOTO ACCESS permission is set to either Selected Photos or All Photos. In my case, I had denied the permission so there was no error log on the console and the image picker was not opening.

PS I know it's not directly related to the SO's question but might be helpful if someone comes across this.

Slick Slime
  • 649
  • 7
  • 19
0

Don't bother about this issue much. This is just a simulator issue(mostly on iOS). Testing this on a real device is advisable.

0

I think it because it using 'pickimage' instead of 'pickMultiImage', so u are only allow to pick 1 image at a time, try to make ur 'imageFile' to null first before you pick another image.

Torinouq J
  • 511
  • 5
  • 5
0

Unfortunately this is now happening with real devices too - I'm using an iPhone 6 version 12.5.5

alfietap
  • 1,585
  • 1
  • 15
  • 38