0

我正在尝试建立一个搜索列表,该列表工作正常,但如果结果为空,我不需要显示任何数据。我尝试了以下代码,但小部件会保持一秒钟然后消失

FutureBuilder(
                    future: getSearchedProducts(widget.searchString),
                    builder: (BuildContext context, AsyncSnapshot snapshot) {
                      if (!snapshot.hasData) {
                        return Center(
                          child: Container(
                            child: Text('No Data Found.'),
                          ),
                        );
                      } else {
                        return ListView.builder(
                            shrinkWrap: true,
                            itemCount: searchResult.length,
                            scrollDirection: Axis.vertical,
                            itemBuilder: (context, index) {

                                  return Card(
                                    child: ListTile(
                                      leading: Image.network(searchResult[index].proThumbnail),
                                      title: Text(searchResult[index].proName),
                                      onTap: () {
                                        print(searchResult[index].proName);
                                        Navigator.push(context, MaterialPageRoute(builder: (context) {
                                          return ProductPage(prodid: searchResult[index].proId);
                                        }));
                                      },
                                    ),
                                  );


                            });
                      }
                    })

谁能帮我这个。先感谢您。

4

2 回答 2

1

我只是编写如下代码并且它可以工作。

 FutureBuilder(
                    future: getSearchedProducts(widget.searchString),
                    builder: (BuildContext context, AsyncSnapshot snapshot) {

                      print('length of list ${searchResult.length}');

                      if (searchResult.length==0) {
                        return Center(
                          child: Text('No data'),
                        );
                      }
                      else if (!snapshot.hasData) {
                        return Center(
                            child: CircularProgressIndicator(),
                        );
                      }
                      else {
                        return ListView.builder(
                            shrinkWrap: true,
                            itemCount: searchResult.length,
                            scrollDirection: Axis.vertical,
                            itemBuilder: (context, index) {

                                  return Card(
                                    child: ListTile(
                                      leading: Image.network(searchResult[index].proThumbnail),
                                      title: Text(searchResult[index].proName),
                                      onTap: () {
                                        print(searchResult[index].proName);
                                        Navigator.push(context, MaterialPageRoute(builder: (context) {
                                          return ProductPage(prodid: searchResult[index].proId);
                                        }));
                                      },
                                    ),
                                  );

                            });



                      }
                    }),

于 2019-10-22T21:12:09.320 回答
0

在你的代码Else部分之前return ListView.builder——添加以下代码。

 ...
 else {
 if (searchResult.length == 0) 
          {
            return Center(child: const Text('No Date'));
          }
 return ListView.builder .....
于 2019-10-22T20:51:10.533 回答