'PlatformException(multiple_request, Cancelled by a second request, null, null) in imagePicker

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



Solution 1:[1]

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.

Solution 2:[2]

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.

Solution 3:[3]

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

Solution 4:[4]

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.

Solution 5:[5]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Obisi7
Solution 2 Dharman
Solution 3 Marc Sanny
Solution 4 user3066199
Solution 5 Slick Slime