skip to Main Content

enter image description here

if we search for any text in the search bar …we get filtered search results… but the issue is that all other To-dos are gone ..

enter image description here

I cant get back to all other Todos ..

search function code

  List<Todos> todList =[];
searchTodo(String enterdText) {
    final search = todList.where((txt) {
      final inputtext = txt.todoText.toLowerCase();
      final input = enterdText.toLowerCase();
      return inputtext.contains(input);
    }).toList();
    setState(() => {todList = search});
  }

search bar

TextField(
              onChanged: searchTodo,
              decoration: InputDecoration(
                  hintText: 'Search',
                  prefixIcon: Icon(Icons.search_rounded),
                  //  enabledBorder: InputBorder.none,
                  iconColor: Colors.grey.shade400,
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(20))),
            ),

2

Answers


  1. Save your list to a different variable and once the search ends replace with the saved list.

    Login or Signup to reply.
  2. Something like this... little bit confuse on list size if not replace with length
    
    List<Todos> todList =[];
    List<Todos> tmpList =[];
    searchTodo(String enterdText){
    If(tmpList.size<todList.size){
    
    tmpList=todList;
    }
    if(enterdText.trim().isEmpty()){
    final search=tmpList;
    
    
    }else{
    final search = todList.where((txt) {
      final inputtext = txt.todoText.toLowerCase();
      final input = enterdText.toLowerCase();
      return inputtext.contains(input);
    }).toList();}
    setState(() => {
    If(tmpList.size<todList.size){
    
    tmpList=todList;
    }
    
    todList = search});
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search