Skip to content

[image_picker] [Android] retrieveLostData raises _CastError if user cancelled choosing a photo #114551

@HertzDevil

Description

@HertzDevil

Steps to Reproduce

  1. Enable "Don't keep activities" in the developer options menu
  2. Execute flutter run on the code sample
  3. Tap on the floating button to launch the image picker
  4. Press the back button, the application should now destroy and restart itself

Then an unhandled exception is thrown with the following stacktrace:

E/flutter (10730): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'Null' is not a subtype of type 'String' in type cast
E/flutter (10730): #0      _CastListBase.[] (dart:_internal/cast.dart:99:46)
E/flutter (10730): #1      ListMixin.elementAt (dart:collection/list.dart:78:33)
E/flutter (10730): #2      ListIterator.moveNext (dart:_internal/iterable.dart:342:26)
E/flutter (10730): #3      ImagePickerAndroid.getLostData (package:image_picker_android/image_picker_android.dart:270:33)
E/flutter (10730): <asynchronous suspension>
E/flutter (10730): #4      _MyAppState._showLostData (package:bug/main.dart:22:33)

Inside the method ImagePickerAndroid.getLostData the result variable has the value CastMap ({path: null, pathList: [null], type: image, imageQuality: 100}). I suspect that ImagePickerDelegate.finishWithSuccess in the Java plugin is producing that list containing the null.

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

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    _showLostData();
  }

  void _showLostData() async => await ImagePicker().retrieveLostData();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        floatingActionButton: FloatingActionButton(
          onPressed: () => ImagePicker().pickImage(source: ImageSource.gallery),
          child: const Icon(Icons.add),
        ),
      ),
    );
  }
}
pubspec.yaml
name: bug
description: A new Flutter project.

publish_to: 'none'

version: 1.0.0+1

environment:
  sdk: '>=2.18.2 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  image_picker: ^0.8.6

flutter:
  uses-material-design: true
pubspec.lock
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
  async:
    dependency: transitive
    description:
      name: async
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.9.0"
  characters:
    dependency: transitive
    description:
      name: characters
      url: "https://pub.dartlang.org"
    source: hosted
    version: "1.2.1"
  collection:
    dependency: transitive
    description:
      name: collection
      url: "https://pub.dartlang.org"
    source: hosted
    version: "1.16.0"
  cross_file:
    dependency: transitive
    description:
      name: cross_file
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.3.3+2"
  flutter:
    dependency: "direct main"
    description: flutter
    source: sdk
    version: "0.0.0"
  flutter_plugin_android_lifecycle:
    dependency: transitive
    description:
      name: flutter_plugin_android_lifecycle
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.0.7"
  flutter_web_plugins:
    dependency: transitive
    description: flutter
    source: sdk
    version: "0.0.0"
  http:
    dependency: transitive
    description:
      name: http
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.13.5"
  http_parser:
    dependency: transitive
    description:
      name: http_parser
      url: "https://pub.dartlang.org"
    source: hosted
    version: "4.0.2"
  image_picker:
    dependency: "direct main"
    description:
      name: image_picker
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.8.6"
  image_picker_android:
    dependency: transitive
    description:
      name: image_picker_android
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.8.5+3"
  image_picker_for_web:
    dependency: transitive
    description:
      name: image_picker_for_web
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.1.10"
  image_picker_ios:
    dependency: transitive
    description:
      name: image_picker_ios
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.8.6+1"
  image_picker_platform_interface:
    dependency: transitive
    description:
      name: image_picker_platform_interface
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.6.2"
  js:
    dependency: transitive
    description:
      name: js
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.6.4"
  material_color_utilities:
    dependency: transitive
    description:
      name: material_color_utilities
      url: "https://pub.dartlang.org"
    source: hosted
    version: "0.1.5"
  meta:
    dependency: transitive
    description:
      name: meta
      url: "https://pub.dartlang.org"
    source: hosted
    version: "1.8.0"
  path:
    dependency: transitive
    description:
      name: path
      url: "https://pub.dartlang.org"
    source: hosted
    version: "1.8.2"
  plugin_platform_interface:
    dependency: transitive
    description:
      name: plugin_platform_interface
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.1.3"
  sky_engine:
    dependency: transitive
    description: flutter
    source: sdk
    version: "0.0.99"
  source_span:
    dependency: transitive
    description:
      name: source_span
      url: "https://pub.dartlang.org"
    source: hosted
    version: "1.9.0"
  string_scanner:
    dependency: transitive
    description:
      name: string_scanner
      url: "https://pub.dartlang.org"
    source: hosted
    version: "1.1.1"
  term_glyph:
    dependency: transitive
    description:
      name: term_glyph
      url: "https://pub.dartlang.org"
    source: hosted
    version: "1.2.1"
  typed_data:
    dependency: transitive
    description:
      name: typed_data
      url: "https://pub.dartlang.org"
    source: hosted
    version: "1.3.1"
  vector_math:
    dependency: transitive
    description:
      name: vector_math
      url: "https://pub.dartlang.org"
    source: hosted
    version: "2.1.2"
sdks:
  dart: ">=2.18.2 <3.0.0"
  flutter: ">=2.10.0"
flutter --version
Flutter 3.3.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision eb6d86ee27 (4 weeks ago) • 2022-10-04 22:31:45 -0700
Engine • revision c08d7d5efc
Tools • Dart 2.18.2 • DevTools 2.15.0

Metadata

Metadata

Labels

P1High-priority issues at the top of the work listc: crashStack traces logged to the consolefound in release: 3.3Found to occur in 3.3found in release: 3.5Found to occur in 3.5has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: image_pickerThe Image Picker plugin.packageflutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyr: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions