简体   繁体   English

搜索栏问题Flutter

[英]Search Bar issue in Flutter

I have no idea why my search bar function does not filter and return the results as per my input.我不知道为什么我的搜索栏 function 没有根据我的输入过滤并返回结果。 What happens right now is that it prints the entire List instead of the desired results and this only occurs while passing the query text entered on the TextField.现在发生的是它打印整个列表而不是所需的结果,并且这仅在传递在 TextField 上输入的查询文本时发生。 No issues when I send in a hardcoded query like say Paneer and it returns all the entries with the word Paneer in them.当我发送像Paneer这样的硬编码查询时没有问题,它返回所有包含单词Paneer的条目。 Below is my code:下面是我的代码:

The method that does the filtering:进行过滤的方法:

Future<void> searchData(String query) async {
    Map<String, dynamic> _popularDishes = {};
    List<dynamic> _searchDish = [];
    List<dynamic> _searchResult = [];
    final url = Uri.parse(baseUrl + 'api/all_products');
    final response = await http.get(url);
    PopularDishes popularDishes = popularDishesFromJson(response.body);
    _popularDishes = popularDishes.toJson();
    _popularDishes['data'].forEach((value) => _searchDish.add(value));
    _searchResult.add(_searchDish.where((element) {
      final name = element['product_name'].toLowerCase();
      final searchQuery = query.toLowerCase();
      return name.contains(searchQuery);
    }));
    print(_searchResult);
  }

The widget with the TextField.带有 TextField 的小部件。 I have marked the TextField line.我已经标记了 TextField 行。 It seems like query is always getting passed as a null String.似乎query总是作为 null 字符串传递。 However, if I try printing query in the onChanged method of TextField, it shows my input exaxtly in the sequence it was entered.但是,如果我尝试在 TextField 的 onChanged 方法中打印查询,它会按照输入的顺序准确显示我的输入。 I believe passing the query string instead of an empty string to the provider method Provider.of<PopularDishesProvider>(context, listen: false).searchData(query) could solve the issue:我相信将查询字符串而不是空字符串传递给提供程序方法Provider.of<PopularDishesProvider>(context, listen: false).searchData(query)可以解决问题:

class SearchState extends State<Search> {
  final _controller = TextEditingController();
  String query = '';

  @override
  Widget build(BuildContext context) {

    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
          titleSpacing: 0,
          toolbarHeight: 100,
          title: Column(
            children: [
              Stack(
                children: [
                  Container(
                    .......
                    .......
                    .......
                    ....... 
                              child: Row(
                                children: [
                                  Flexible(
                                      flex: 9,
                                      fit: FlexFit.tight,
                                      child: Center(
                                        child: TextField(         //This here is the TextField
                                          controller: _controller,
                                          onChanged: (value) {    
                                            query = value;
                                            Provider.of<PopularDishesProvider>(       //I'm probably doing something wrong here
                                                    context,
                                                    listen: false)
                                                .searchData(query);
                                          },
                                          autofocus: true,
                                          cursorColor: Colors.grey,
                                          style: const TextStyle(
                                              color: Colors.grey, fontSize: 18),
                                          decoration: const InputDecoration(
                                            border: InputBorder.none,
                                            hintText:
                                                'Search By Restaurant or Food',
                                            hintStyle:
                                                TextStyle(color: Colors.grey),
                                            // suffixIcon: Icon(
                                            //   Icons.send,
                                            //   color: Colors.grey,
                                            //   size: 20,
                                            // )
                                          ),
                                        ),
                                      )),
                                  .....
                                  .....
                                  .....  
                          ],
                        )
                      ],
                    ),
                  ),
                ],
              )
            ],
          )),
      body: Column(
        .......
        .......
        .......
      ),
    );
  }
}

I'm open to all sorts of ideas and suggestions.我乐于接受各种想法和建议。 The output I get now is vs the Output I should get:我现在得到的 output 与我应该得到的 Output 相比:

The current Output prints everything as mentioned earlier当前的 Output 打印前面提到的所有内容

[
        {
            "product_id": 9,
            "restaurant_name": "Aminia",
            "restaurant_id": "17",
            "product_name": "Club Sandwich",
            "product_description": "Cool and new tasty biggg sandwich",
            "product_image": "/public/assets/product/aoVjKa12-14-38.jpg",
            "product_selling_price": "120"
        },
        {
            "product_id": 8,
            "restaurant_name": "Mocambo",
            "restaurant_id": "6",
            "product_name": "Kaju Paneer",
            "product_description": "Tasty yummy paneer gravy dish",
            "product_image": "/public/assets/product/lgml5L03-19-41.jpg",
            "product_selling_price": "320"
        },
        {
            "product_id": 7,
            "restaurant_name": "City Club",
            "restaurant_id": "1",
            "product_name": "Tibetan Soup",
            "product_description": "Healthy Soup from the mountains of Tibet",
            "product_image": "/public/assets/product/CgMBpm02-03-38.jpg",
            "product_selling_price": "120"
        },
        {
            "product_id": 6,
            "restaurant_name": "City Club",
            "restaurant_id": "1",
            "product_name": "Jersey Burger",
            "product_description": "Tasty yummy burgir. BURGIRRRRR",
            "product_image": "/public/assets/product/1Xf0sr01-43-20.jpg",
            "product_selling_price": "185"
        },
        {
            "product_id": 5,
            "restaurant_name": "City Club",
            "restaurant_id": "1",
            "product_name": "Palak Paneer",
            "product_description": "Tasty silky gravy with goodness of palak",
            "product_image": "/public/assets/product/C6pGz101-42-17.jpg",
            "product_selling_price": "180"
        },
        {
            "product_id": 4,
            "restaurant_name": "City Club",
            "restaurant_id": "1",
            "product_name": "Shahi Paneer",
            "product_description": "Tasty Paneer main course dish",
            "product_image": "/public/assets/product/vgI1dR01-29-18.jpg",
            "product_selling_price": "240"
        }
    ]

The desired output:所需的 output:

[
  {
            "product_id": 8,
            "restaurant_name": "Mocambo",
            "restaurant_id": "6",
            "product_name": "Kaju Paneer",
            "product_description": "Tasty yummy paneer gravy dish",
            "product_image": "/public/assets/product/lgml5L03-19-41.jpg",
            "product_selling_price": "320"
        },
   {
            "product_id": 5,
            "restaurant_name": "City Club",
            "restaurant_id": "1",
            "product_name": "Palak Paneer",
            "product_description": "Tasty silky gravy with goodness of palak",
            "product_image": "/public/assets/product/C6pGz101-42-17.jpg",
            "product_selling_price": "180"
        },
    {
            "product_id": 4,
            "restaurant_name": "City Club",
            "restaurant_id": "1",
            "product_name": "Shahi Paneer",
            "product_description": "Tasty Paneer main course dish",
            "product_image": "/public/assets/product/vgI1dR01-29-18.jpg",
            "product_selling_price": "240"
        } 
]

Might be completely off, there somethings not clear in your questions but, I think you have to closely look at the state and how it's changing, it seems you are using a provider for the state. Check what happens when the search method is called and if the widget rebuilds how that affects the state. I also don't see the search data method returning any list of results how are you getting them?可能完全关闭,您的问题中有一些不清楚的地方,但是,我认为您必须仔细查看 state 及其变化,看来您正在使用 state 的提供程序。检查调用搜索方法时会发生什么,如果小部件重建如何影响 state。我也没有看到搜索数据方法返回任何结果列表,你是如何得到它们的?

Have you tried using flutter search delegate?您是否尝试过使用 flutter 搜索代理?

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

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