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

1,313

Solution 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

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.

Share:
1,313
Obisi7
Author by

Obisi7

Updated on December 02, 2022

Comments

  • Obisi7
    Obisi7 over 1 year

    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
      Obisi7 about 2 years
      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
    Obisi7 about 2 years
    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
    Obisi7 about 2 years
    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