flutter / flutter

Flutter makes it easy and fast to build beautiful apps for mobile and beyond
https://flutter.dev
BSD 3-Clause "New" or "Revised" License
165.57k stars 27.34k forks source link

"sending message to a Handler on a dead thread" after image selected using image_picker library #153244

Closed akshaykap11 closed 1 month ago

akshaykap11 commented 2 months ago

Steps to reproduce

Device Details:

Device Model: OnePlus A5000 Android Version: 9.0 (Pie) Flutter Version: Flutter 3.22.2 • channel stable • https://github.com/flutter/flutter.git Framework • revision 761747bfc5 (10 weeks ago) • 2024-06-05 22:15:13 +0200 Engine • revision edd8546116 Tools • Dart 3.4.3 • DevTools 2.34.3

Image Capture Package: [Specify the version of the image_picker package you are using]

1) Open pubspec.yaml and add the image_picker package. 2) Implement image capture functionality using the image_picker package. 3) After capturing the image, a tick mark appears, indicating that the image should be selected/picked. 4) However, no image is selected/picked, and the following error is logged: 5) E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: sending message to a Handler on a dead thread

Expected results

The captured image should be selected and returned by the image_picker package.

Actual results

2) However, no image is selected/picked, and the following error is logged: 2) E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: sending message to a Handler on a dead thread

Code sample

Future<void> getImage() async {
    try {
      await _requestPermissions();
      final ImagePicker picker = ImagePicker();
      final XFile? image = await picker.pickImage(source: ImageSource.camera);

      if (image != null) {
        setState(() `{`
          selectedImage = File(image.path);
        });
      } catch (e, stackTrace) {
            print("Error getting image: $e");
          }
        }
}

Screenshots or Video

Screenshots / Video demonstration [Upload media here]

Logs

Logs ```console [Paste your logs here] ```

Flutter Doctor output

[√] Flutter (Channel stable, 3.22.2, on Microsoft Windows [Version 10.0.19045.4651], locale en-IN) [√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android Studio (version 2022.2) [√] Connected device (4 available) [√] Network resources

iapicca commented 2 months ago

I cannot reproduce the issue with the latest stable, do you experience the problem with the code sample below?

code sample ```dart import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; typedef PickImageCallback = void Function(XFile); void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(context) => const MaterialApp(home: MyHomePage()); } class MyHomePage extends StatefulWidget { const MyHomePage({super.key}); @override State createState() => _MyHomePageState(); } class _MyHomePageState extends State { late final ValueNotifier _notifier; @override void initState() { _notifier = ValueNotifier(null); super.initState(); } @override void dispose() { _notifier.dispose(); super.dispose(); } void _pickImage(XFile file) => _notifier.value = file; @override Widget build(context) => ValueListenableBuilder( valueListenable: _notifier, builder: (context, crossFile, child) => Scaffold( body: crossFile == null ? PickImage(callback: _pickImage) : Center( child: FutureBuilder( future: crossFile.readAsBytes(), builder: (context, snapshot) { if (snapshot.connectionState != ConnectionState.done) { return const CircularProgressIndicator(); } if (snapshot.data == null || snapshot.hasError || snapshot.data!.isEmpty) { return const Icon(Icons.error); } return Image.memory(snapshot.data!); }, ), ), floatingActionButton: crossFile == null ? null : FloatingActionButton( onPressed: () => _notifier.value = null, child: const Icon(Icons.restore), ), ), ); } class PickImage extends StatelessWidget { final PickImageCallback callback; const PickImage({ super.key, required this.callback, }); @override Widget build(context) => Column( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.center, children: [ Center( child: PickerButton( icon: Icons.camera, callback: callback, source: ImageSource.camera, ), ), Center( child: PickerButton( icon: Icons.folder, callback: callback, source: ImageSource.gallery, ), ), ], ); } class PickerButton extends StatelessWidget { final ImageSource source; final PickImageCallback callback; final IconData icon; const PickerButton({ super.key, required this.source, required this.callback, required this.icon, }); @override Widget build(context) => OutlinedButton( onPressed: () async { final file = await ImagePicker().pickImage( source: source, ); if (file == null) { return; } callback(file); }, child: Icon(icon), ); } ``` ```yaml name: issue_153244 description: "A new Flutter project." publish_to: 'none' version: 1.0.0+1 environment: sdk: ^3.5.0 dependencies: flutter: sdk: flutter image_picker: ^1.1.2 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^4.0.0 flutter: uses-material-design: true ``` ```console [✓] Flutter (Channel stable, 3.24.0, on macOS 14.5 23F79 darwin-arm64, locale en-US) • Flutter version 3.24.0 on channel stable at /Users/francesco/fvm/versions/stable • Upstream repository https://github.com/flutter/flutter.git • Framework revision 80c2e84975 (12 days ago), 2024-07-30 23:06:49 +0700 • Engine revision b8800d88be • Dart version 3.5.0 • DevTools version 2.37.2 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/francesco/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 15.4) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15F31d • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2022.3) • 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 17.0.6+0-17.0.6b829.9-10027231) [✓] VS Code (version 1.92.1) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.94.0 [✓] Connected device (3 available) • macOS (desktop) • macos • darwin-arm64 • macOS 14.5 23F79 darwin-arm64 • Mac Designed for iPad (desktop) • mac-designed-for-ipad • darwin • macOS 14.5 23F79 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 120.0.6099.71 [✓] Network resources • All expected network resources are available. • No issues found! ```
akshaykap11 commented 2 months ago

I'm facing this issue specifically for this device version Device Model: OnePlus A5000 Android Version: 9.0 (Pie)

huycozy commented 2 months ago

@akshaykap11 It seems you mistyped the package name as I don't see image_capture on pub.dev. So I updated the description to image_picker instead (based on the issue title). Please correct me if I'm wrong (in case you have a private package named image_capture)

When the issue happens, is the app terminated? Can you provide the entire crash stack trace?

Also, what is the current image_picker version? Can you confirm with the sample code Iapicca shared above?

akshaykap11 commented 2 months ago

The app is not terminated. The flow is something like this 1) I click the cta action 2) camera preview opens 3) i click the pic which shows me a tick icon with the captured image. 4) once i click the tick icon, i get back to the app 5) Here expectation - image captured should show in the image container actual results - placeholder is still there

-> I dont have the device with me now. Will share the entire crash stack trace in few days once I get the device -> Image picker version - image_picker: ^1.1.2

-> Also will try the sample code with the device and update here again

github-actions[bot] commented 1 month ago

Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. If you find this problem please file a new issue with the same description, what happens, logs and the output of 'flutter doctor -v'. All system setups can be slightly different so it's always better to open new issues and reference the related ones. Thanks for your contribution.

github-actions[bot] commented 1 month ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.