Home > OS >  Flutter/Dart Filter a Objekt from List
Flutter/Dart Filter a Objekt from List

Time:12-03

i wanna filter a List with a Button String, I got a Content List like this,

 var aktifliste = [];   
 List<Dersler> icerik2 = [
        Dersler("TYT", "Türkce-TYT", "Sözückte Anlam", "https://youtube.de"),
        Dersler("TYT", "Matematik-TYT", "Sayilar", "https://google.de"),
        Dersler("TYT", "Fizik-TYT", "Madde", "https://kkspro.de"),
      ];

and the Class from List is,

class Dersler {
  String sinav;
  String ders;
  String konu;
  String link;

  Dersler(this.sinav, this.ders, this.konu, this.link);
  Map toJson() => {
        'sinav': sinav,
        'ders': ders,
        'konu': konu,
        'link': link,
      };
}

the Function for Filter the List and insert to another list is ,

void filtre(String gelen) {
    aktifliste = icerik2.where((element) => element == gelen).toList();
    update();
  }

if i click the Button that is starting the filtre Function and sending the "gelen" String. and the aktifliste will be inserted with a Filtered Objects, but i wanna filter this with a Option , example if i send TYT then it will be filtered with sinav Option.

ElevatedButton(
                onPressed: () {
          controller.filtre("TYT"),
}, child: Text(controller.icerik2[index].ders));

if i send the String "Sayilar" it will be make a search the all List and find only one Video.

ElevatedButton(
                onPressed: () {
controller.filtre("TYT"),
}, child: Text(controller.icerik2[index].ders));

Has somebody have any Idea about the Filtering or searching an Object in the List ? its not working with where Function. not returning any Object ?

Thanks !

CodePudding user response:

See if this helps you.

Object:

class Dersler {
  String sinav;
  String ders;
  String konu;
  String link;

  Dersler(this.sinav, this.ders, this.konu, this.link);
  Map toJson() => {
        'sinav': sinav,
        'ders': ders,
        'konu': konu,
        'link': link,
      };

  bool compareTo(String filter){
    if(sinav == filter) return true;
    if(ders.contains(filter)) return true;
    if(konu.contains(filter)) return true;
    return false;
  }
}

Function filter

filter(String gelen) async {
  var aktifliste = icerik2.where((element) => element.compareTo(gelen)).toList();
}
  • Related