Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 940
Obisi7 Avatar asked Jan 17 '26 03:01

Obisi7


2 Answers

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

like image 159
Theo Avatar answered Jan 19 '26 19:01

Theo


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.

like image 36
Obisi7 Avatar answered Jan 19 '26 20:01

Obisi7



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!