Home > Back-end >  How can I make my search history not have the same elements repeated many times in flutter/dart?
How can I make my search history not have the same elements repeated many times in flutter/dart?

Time:06-05

I'm trying to do a search history using a search delegate but I'm having a problem. When I perform a search, that element can appear several times in the history and what I want is that it not be repeated.

If I search 3 times for the same person, in the search history it appears 3 times And I only want it to appear once.

How could I do it?

help would be appreciated.

Code and image::

class MPState extends State<MP>{
    
    @override
      Widget build(BuildContext context) {
        return DefaultTabController(
            length: 3,
            child: new Scaffold(
              resizeToAvoidBottomInset : false,
              appBar: AppBar(
                title: Text("App"),
                backgroundColor: Colors.redAccent,
                elevation: 0,
               actions: [
                 IconButton(
                     icon: Icon(Icons.search),
                     onPressed: () async{
                       final  busqueda = await showSearch(
                           context: context,
                           delegate: SearchDelegateMP("Buscar...",this.historialMenuPrincipal)
                       );
    
                       if(busqueda != null ) {
                     if (this.historialMenuPrincipal.length > 0) {
                     //  this.historialMenuPrincipal.add(busqueda);
                       /*historialMenuPrincipal.removeWhere((item) =>
                       item.email == busqueda.email); // si elimina*/
                       for (int i = 0; i < historialMenuPrincipal.length; i  ) {
                        if(historialMenuPrincipal[i].email== busqueda.email){
                         
                          print(historialMenuPrincipal[i].email);
                          break;
                        }else{
                          this.historialMenuPrincipal.add(busqueda);
                          
                          break;
                        }
                       }
                      
                     }else{  this.historialMenuPrincipal.add(busqueda);}
                   }
    }
    class SearchDelegateMPextends SearchDelegate<SearchDelegateM>{
    
    
     @override
      List<Widget> buildActions(BuildContext context) {
        return [
               //code
               ];
      }
    
    
     @override
      Widget buildResults(BuildContext context) {
    
    //code
    }
    
    
     Widget buildSuggestions(BuildContext context) {
    return StatefulBuilderSuggestions(context,this.historialMenuPrincipal);
    }
    
    Widget StatefulBuilderSuggestions(BuildContext context ,List <SearchDelegateM> historialMenuPrincipal){
     
        return Container(
          child:StatefulBuilder(
            builder:(context,setState)
        {
    return Container(
      child: ListView.builder(
          itemCount: historialMenuPrincipal.length,
        
          itemBuilder: (context,i)
          {
            contentPadding: EdgeInsets.symmetric(vertical: 12,horizontal: 16);
            leading:CircleAvatar(
              radius: 32,
              backgroundImage: NetworkImage(
                  "https://2.bp.blogspot.com/-3ZzNt8ZsjQk/WR9W4IFn4II/AAAAAAAAAJw/_inTVynhS60V7F5IZ-461-pda7WArTStwCEw/s1600/ANA.jpg"),
            );
            return
              ListTile(
           
              title: Text(historialMenuPrincipal[i].email ), 
              
               
             trailing:  IconButton(
          icon: Icon(Icons.cancel,color: Colors.black,),
            onPressed: () {
          setState(() {
          historialMenuPrincipal.remove(historialMenuPrincipal[i]);
          });
          },)
              );
          }
      ),
    
    );
        }
          )
        );
    
    }

enter image description here

CodePudding user response:

Empty your list with every new search, before you start adding to it.

this.historialMenuPrincipal.clear();

What is happening is that the result is being added n number of times, even if the result is already there from previous searches. N = the number of times the search is matched.

CodePudding user response:

List can have repeated elements. You can parse your list to Set as set only contains unique elements.

List <SearchDelegateM> uniqueElementsList = historialMenuPrincipal.toSet().toList();

use this code before showing your elements in Listview.builder() and use uniqueElementsList in your builder.

  • Related