'Flutter ImagePicker Ios Simulator issue

I use dependencies ImagePicker in Flutter with iOS Simulator and I have an issue when an image is picked from the library.

When a selected an image, nothing is returned and naturally, when I try a second time, I have an error: PlatformException (PlatformException(multiple_request, Cancelled by a second request, null, null))

It worked very well but when I update flutter to 2.10.8, the issues appear. I don't change anything except update flutter.

I try to downgrade flutter but it's the same thinks. The permissions are ok in info.plist and Podfile, Xcode is updated.

    <string>photos</string>
 ## dart: PermissionGroup.photos
         'PERMISSION_PHOTOS=1',

[✓] Flutter (Channel stable, 2.10.4, on macOS 12.2.1 21D62 darwin-arm, locale fr-FR)
    • Flutter version 2.10.4 at /Users/yasbad/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c860cba910 (il y a 3 semaines), 2022-03-25 00:23:12 -0500
    • Engine revision 57d3bac3dd
    • Dart version 2.16.2
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/yasbad/Library/Android/sdk
    • Platform android-32, build-tools 32.1.0-rc1
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7772763)

[✓] VS Code (version 1.66.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.38.1

• No issues found!

Here is the complete simple code that is causing the problem:

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class BugImagePicker extends StatefulWidget {
  const BugImagePicker({
    Key? key,
  }) : super(key: key);

  @override
  State<BugImagePicker> createState() {
    return BugImagePickerState();
  }
}

class BugImagePickerState extends State<BugImagePicker> {
  Future<XFile?>? file;
  String? base64Image;
  XFile? tmpFile;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: const Text("Bug Image Picker"),
        ),
        body: Column(
          children: [
            OutlinedButton(
              onPressed: chooseImage,
              child: const Text('Choose Image'),
            ),
            const SizedBox(height: 10.0),
            showImage(),
          ],
        ));
  }

  chooseImage() async {
    setState(() {
      file = ImagePicker.pickImage(source: ImageSource.gallery);
    });
  }

  Widget showImage() {
    return FutureBuilder<XFile?>(
      future: file,
      builder: (BuildContext context, AsyncSnapshot<XFile?> snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {}
        if (snapshot.connectionState == ConnectionState.done &&
            null != snapshot.data) {
          return SizedBox(
            width: double.maxFinite,
            child: Image.file(
              File(snapshot.data!.path),
              height: 200,
              fit: BoxFit.cover,
            ),
          );
        } else if (null != snapshot.error) {
          return const Text(
            'Error Picking Image',
            textAlign: TextAlign.center,
          );
        } else {
          return const Text(
            'No Image selected',
            textAlign: TextAlign.center,
          );
        }
      },
    );
  }
}






Sources

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

Source: Stack Overflow

Solution Source