简体   繁体   English

条件语句不适用于 ListTile onTap Flutter

[英]Conditional statement not working on ListTile onTap Flutter

I'm trying to put a condition for image opener in my app.我正在尝试在我的应用程序中设置图像打开器的条件。 If image is null then it shouldn't do anything but if there is some image coming then it should open it in full screen image opener but its not working.如果imagenull那么它不应该做任何事情但是如果有一些图像来那么它应该在全屏图像打开器中打开它但它不起作用。 I'm fetching data in ListView.builder .我在ListView.builder中获取数据。 Not all images are going to be null from database because the image is optional so some images will be coming, some will be not I can't fetch them all and put them into the image viewer it'll show broken image view which I don't want.并非所有图像都会是数据库中的 null,因为图像是可选的,所以有些图像会出现,有些不是不想。

Update: Debug log gives this when I tap on no image ListTile index更新:当我点击没有图像 ListTile 索引时,调试日志给出了这个

Image provider: NetworkImage("", scale: 1.0)
Image key: NetworkImage("", scale: 1.0)

打印快照给出这个

Here's my ListTile:这是我的 ListTile:

ListTile(
                        onTap: () async {
                          if (snapshot.data?.docs[index].data()['image'] !=
                              null) {
                            Navigator.of(context).push(MaterialPageRoute(
                                builder: (context) => ImageOpener(
                                      imageProvider: Image.network(
                                        snapshot.data?.docs[index]
                                            .data()['image'],
                                        errorBuilder:
                                            (context, error, stackTrrace) {
                                          return const Icon(Icons.error);
                                        },
                                      ).image,
                                    )));
                          } else if (snapshot.data?.docs[index]
                                  .data()['image'] ==
                             "") {
                            ScaffoldMessenger.of(context).showSnackBar(
                                const SnackBar(
                                    content: Text('No image to show')));
                          } else {
                            ScaffoldMessenger.of(context).showSnackBar(
                                const SnackBar(
                                    content: Text('No image to show')));
                          }
                        },
                        subtitle: Text(
                          snapshot.data?.docs[index].data()['notificationbody'],
                          style: const TextStyle(
                              // fontSize: 15,
                              ),
                        ),
                        title: Text(snapshot.data?.docs[index]
                                ['notificationtext'] ??
                            "Loading..."),
                        leading: const Icon(Icons.notifications),
                        trailing: Image.network(
                          snapshot.data?.docs[index].data()['image'],
                          errorBuilder: (context, error, stackTrrace) {
                            return const Visibility(
                              visible: false,
                              child: Padding(
                                padding: EdgeInsets.all(8.0),
                                child: Icon(Icons.error),
                              ),
                            );
                          },
                        ),
                      ),

it is because data() returns an abject you should convert it to a map这是因为data()返回一个卑鄙的人,您应该将其转换为 map

var data = snapshot.data?.docs[index].data() as Map<String,dynamic>;

then you can check now if data['image'] is null那么您现在可以检查data['image']是否为 null

Check if the image url is not empty (not null) and if it is not empty you can call your Navigator.push检查图像 url 是否不为空(不为空),如果不为空,您可以调用 Navigator.push

if (snapshot.data?.docs[index].data()['image'].isNotEmpty) {
                            Navigator.of(context).push(MaterialPageRoute(
                                builder: (context) => ImageOpener(
                                      imageProvider: Image.network(
                                        snapshot.data?.docs[index]
                                            .data()['image'],
                                        errorBuilder:
                                            (context, error, stackTrrace) {
                                          return const Icon(Icons.error);
                                        },
                                      ).image,
                                    )));
                          }else{
                            ScaffoldMessenger.of(context).showSnackBar(
                                const SnackBar(
                                    content: Text('No image to show')));
                          } 

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

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