So I have this non conventional code that I had to put in to make the container inside of a list tile have image selected with ImagePicker. Here is the snippet:

File _smallImage;

Here is the function for ImagePicker:

 Future _getSmallImage() async {
PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery);
setState(() {
  if (pickedFile != null) {
    _smallImage = File(_smallImage.path);
  } else {
    print('No image selected.');
  }
});

}

Now, here is where it gets a bit confusing, here is the ListTile code:

child: ListTile(

                  trailing: Container(
                    height: 120.0,
                    width: 100.0,
                    //color: Colors.white,
                    decoration: BoxDecoration(
                      color: Colors.white,
                       FileImage(File(_smallImagepath)) : null
                      image: DecorationImage(
                        image: _smallImage == null
                            ? MemoryImage(kTransparentImage)
                            : FileImage(_smallImage),
                        fit: BoxFit.cover,

                      ),
                    ),

                    child: IconButton(

                      icon: Icon(Icons.add),
                      file u path,

                      onPressed: () => _getSmallImage(),
                    ),
                  ),

As you can see, there is an IconButton in the ListTile (I had to do it like this since the list tile also has the onPressed function, and this was the only way for both the small button and the list tile itself be pressed). It has the _getSmallImage() ImagePicker function that gets the image. Up there, there is the DecorationImage that takes that picked Image:

image: DecorationImage(
                        image: _smallImage == null
                            ? MemoryImage(kTransparentImage)
                            : FileImage(_smallImage),
                        fit: BoxFit.cover,

but the thing is, I am getting the 'called on null' error, but I am not sure what is causing it since I have a ternary in case of null:

The getter 'path' was called on null.

The error happens when I try to choose the image from the gallery. I know it is a bit long, but it isn't really that much code, I just wanted to explain the issue as best as I can. Thank you!


Solution 1: Donfreddy

You must use pickedFile.path and not _smallImage

setState(() {
  if (pickedFile != null) {
    _smallImage = File(pickedFile.path); // Change this line.
  } else {
    print('No image selected.');
  }
});