2

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

图像提供者文件:

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 飞镖文件:

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,
                                  ),
                                ),

从画廊或相机中挑选图像的模拟器屏幕截图

4

1 回答 1

0

嗨,请看一下这个讨论: https ://github.com/flutter/flutter/issues/70436

  • 图像选择器包站点上,我们可以看到这是一个众所周知的苹果模拟器问题。我会说它应该在真实设备上为您工作(或尝试仅使用 iOS 模拟器照片中的特定图片对其进行测试)

在此处输入图像描述

于 2022-03-03T15:00:07.163 回答