Skip to content

[image_picker ]The minimal code sample could be improved to handle when no image is picked  #65914

@brian-dunn

Description

@brian-dunn

This bug is easy to reproduce. This run this code that is listed as example code in the Readme for this package. Then, click the floating action button to open the camera on the emulator, but do not take or accept a picture. Instead, click the back button to return to the previous screen. Flutter will then crash with the following error:

**Unhandled Exception: NoSuchMethodError: The getter 'path' was called on null.
E/flutter (12402): Receiver: null
E/flutter (12402): Tried calling: path
E/flutter (12402): #0      Object.noSuchMethod  (dart:core-patch/object_patch.dart:51:5)**

Here is the sample code I ran -- an exact copy/paste from the readme of this package. I am running Flutter 1.20.2 on the stable channel. Also, the sample code provided with package requires importing the dart.io package, so I added that.

import 'package:image_picker/image_picker.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  File _image;
  final picker = ImagePicker();

  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.camera);

    setState(() {
      _image = File(pickedFile.path);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Image Picker Example'),
      ),
      body: Center(
        child: _image == null
            ? Text('No image selected.')
            : Image.file(_image),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: getImage,
        tooltip: 'Pick Image',
        child: Icon(Icons.add_a_photo),
      ),
    );
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    d: examplesSample code and demosgood first issueRelatively approachable for first-time contributorsp: image_pickerThe Image Picker plugin.packageflutter/packages repository. See also p: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions