1

Let me explain .. when I do a search in my search bar it works but I can't go back in the search bar, more specifically when I erase the text it doesn't go back.

Here is my function to filter. displayProduct shows me all the data from my data source (nb: model created from JSON data)

  void searchProduct(String query) {
    final response = displayProduct?.where((element) {
      final titleProduct = element.title?.toLowerCase();
      final input = query.toLowerCase();

      return titleProduct!.contains(input);
    }).toList();
    setState(() {
      displayProduct = response;
    });
  }

Here The textField in which I am searching

TextField(
       onChanged: searchProduct,
       decoration: InputDecoration(
            prefixIcon: Icon(Icons.search_outlined),
            hintText: "Product title",
            border: OutlineInputBorder(
                  borderRadius: BorderRadius.circular(10),
                  borderSide: const BorderSide(
                  color: Color.fromARGB(255, 228, 179, 63)
                  )
            ),
       ),
)
  • Here is the screen before typing

Here is the screen before typing

  • Here When i search it works

Searched

  • But if I erase the text in the search bar there is no return to the starting screen

if I erase the text is no return to the starting screen

  • and if I try to re-type a text no more items are displayed

if I try to re-type

1 Answer 1

1

Please refer the below code

 void searchProduct(String query) {
        if(query.isNotEmpty){
        final response = displayProduct?.where((element) {
          final titleProduct = element.title?.toLowerCase();
          final input = query.toLowerCase();
    
          return titleProduct!.contains(input);
        }).toList();
        setState(() {
          displayProduct = response;
        });
        } else{
    setState(
           //your api data list
    );
            }
              }
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.