imagePicker 中的 PlatformException(multiple_request, 由第二个请求取消, null, null)

Obi*_*si7 15 flutter imagepicker changenotifier

我正在使用 Riverpod 提供程序类来处理从图库中选取图像。但是,一旦选择了图像,我就会收到错误:PlatformException(multiple_request,由第二个请求取消 null,null)。不确定第二个请求来自哪里。更重要的是,由于这种未知的取消,没有图像应用于我的占位符(CircleAvartar)。这是有问题的两个 dart 文件,感谢您的帮助。

图像提供者文件:

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();
  }
Run Code Online (Sandbox Code Playgroud)

AuthScreen dart 文件:

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,
                                  ),
                                ),
Run Code Online (Sandbox Code Playgroud)

用于从图库或相机中选取图像的模拟器屏幕截图

The*_*heo 7

我有最新的 Flutter 3.3.9 和 Xcode 14.1,但这仍然是一个问题。阅读此问题后,解决方法非常简单。使用 image_picker 时,请勿选择第一张图像(带有粉红色花朵):

避免选择该图像


Obi*_*si7 2

除了我之前的回答以及在模拟器环境中与开发人员进一步调整之外,我刚刚发现上传的图像确实在重新加载/重新启动时显示。很奇怪,但如果您必须在模拟模式下进行测试,则可以使用。只需重新启动即可显示上传的图像。恕我直言,这仍然是一个模拟器问题。