flutter 搜索功能无法正常工作

kdfy810k  于 2023-03-24  发布在  Flutter
关注(0)|答案(1)|浏览(130)

我已经显示加载在其他条件下,如果有搜索控制器包含文本,它显示该卡,否则它显示加载在每一个空卡,如果只有一个项目比13加载显示,也显示加载,我讨论添加空间现在,而不是空间,它已经加载现在我想,如果有搜索控制器不包含文本,那么它显示一个加载,但我的代码显示14(长度)加载😥,也是我我想在安排项目,不显示加载,如果有一个项目是目前我想你明白得很好Error

ListView.builder(
                            itemCount: data.length,
                            itemBuilder: (BuildContext context, int index) {
                              final video = data[index];
                              print("Length is ${snapshot.data!.length}");
                             try {
                               if (_searchController.text
                                   .toString()
                                   .isEmpty) {
                                 return InkWell(
                                   onTap: () {
                                     Navigator.push(
                                       context,
                                       MaterialPageRoute(
                                         builder: (builder) =>
                                             video_detail(
                                               data: video['snippet']["publishTime"],
                                               title: video['snippet']["title"],
                                               videoId: video["id"]["videoId"],
                                               channelTitle: video["snippet"]["channelTitle"],
                                               descrption: video["snippet"]["description"],
                                               thumbnail: video['snippet']['thumbnails']['medium']['url'],
                                             ),
                                       ),
                                     );
                                   },
                                   child: Card(
                                     margin: const EdgeInsets.all(8),
                                     color: AppColor.grey200,
                                     child: Column(
                                       crossAxisAlignment: CrossAxisAlignment
                                           .start,
                                       children: [
                                         Image.network(
                                           video['snippet']['thumbnails']['medium']['url'],
                                           width: MediaQuery
                                               .of(context)
                                               .size
                                               .width * 1,
                                         ),
                                         Padding(
                                           padding: const EdgeInsets.all(8),
                                           child: Column(
                                             crossAxisAlignment: CrossAxisAlignment
                                                 .start,
                                             children: [
                                               Text(
                                                 video['snippet']['title'],
                                                 style: const TextStyle(
                                                   fontWeight: FontWeight.bold,
                                                   fontSize: 18,
                                                 ),
                                               ),
                                             ],
                                           ),
                                         ),
                                       ],
                                     ),
                                   ),

                                 );
                               }
                               else if (video['snippet']['title']
                                   .toString()
                                   .toLowerCase()
                                   .contains(_searchController.text.toString()
                                   .trim()
                                   .toLowerCase()) ||
                                   video['snippet']['description']
                                       .toString()
                                       .toLowerCase()
                                       .contains(_searchController.text.toString()
                                       .trim()
                                       .toLowerCase())
                               ) {
                                 return Padding(
                                   padding: const EdgeInsets.all(10.0),
                                   child: Card(
                                     child: ListTile(
                                       leading: Image.network(
                                         video['snippet']['thumbnails']['medium']['url'],
                                         width: 50,
                                       ),
                                       title: Text(video['snippet']['title']),
                                       trailing: Icon(Icons.arrow_forward),
                                       onTap: () {
                                         Navigator.push(
                                           context,
                                           MaterialPageRoute(
                                             builder: (builder) =>
                                                 video_detail(
                                                   data: video['snippet']["publishTime"],
                                                   title: video['snippet']["title"],
                                                   videoId: video["id"]["videoId"],
                                                   channelTitle: video["snippet"]["channelTitle"],
                                                   descrption: video["snippet"]["description"],
                                                   thumbnail: video['snippet']['thumbnails']['medium']['url'],
                                                 ),
                                           ),
                                         );
                                       },
                                     ),
                                   ),
                                 );
                               }
                               else{
                                 return Center(
                                   child: LoadingAnimationWidget.fourRotatingDots(
                                     color: AppColor.grey,
                                     size: 200,
                                   ),
                                 );
                               }
                             }
                             catch(e){
                               print(e);
                             }
                            },
                          ),
xzlaal3s

xzlaal3s1#

我不认为你需要这个加载。加载在等什么?要么你找到要么没有。你应该在列表之前过滤数据。你找到14是因为你没有过滤列表。
我认为一个更好的方法是这样的。创建一个方法,如果输入中有文本,它会过滤数据。

List get filteredData {
  final text = _searchController.text;
  if (text.isEmpty) return data;
  return data.where((element) {
    // your condition where data contains text
  }).toList();
}

然后,在ListView中只显示过滤后的数据,你可以检查filteredData.isEmpty之前,所以你可以显示一个消息,如“No video found”。

ListView.builder(
  itemCount: filteredData.length,
  itemBuilder: (BuildContext context, int index)
    final video = filteredData[index];
  // ...
)

相关问题