I have no idea why my search bar function does not filter and return the results as per my input. 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. No issues when I send in a hardcoded query like say Paneer and it returns all the entries with the word Paneer in them. 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. I have marked the TextField line. It seems like query is always getting passed as a null String. However, if I try printing query in the onChanged method of TextField, it shows my input exaxtly in the sequence it was entered. 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:

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:

The current Output prints everything as mentioned earlier

[
        {
            "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:

[
  {
            "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"
        } 
]


Solution 1: Rodrick Vy

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?

Have you tried using flutter search delegate?