简体   繁体   English

在 Flutter 中将图像添加到 ListTile - 特定问题

[英]Adding image to ListTile in Flutter - specific issue

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.所以我有这个非传统的代码,我必须把它放在列表磁贴内的容器中,使用 ImagePicker 选择图像。 Here is the snippet:这是片段:

File _smallImage;

Here is the function for ImagePicker:这是 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:现在,这里有点令人困惑,这是 ListTile 代码:

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).如您所见,ListTile 中有一个 IconButton(我必须这样做,因为列表磁贴也具有 onPressed 功能,这是小按钮和列表磁贴本身都被按下的唯一方法)。 It has the _getSmallImage() ImagePicker function that gets the image.它具有获取图像的 _getSmallImage() ImagePicker 函数。 Up there, there is the DecorationImage that takes that picked Image:在那里,有一个 DecorationImage 接受选择的图像:

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!谢谢!

You must use pickedFile.path and not _smallImage您必须使用pickFile.path而不是_smallImage

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM