Home > OS >  Find values in the list - Flutter
Find values in the list - Flutter

Time:10-27

How can I return values filtering from this list?

I used contains, but it displays all the values.

I want to retrieve items from the list that have the word "abc" like this -> abc Item. The ones that have other words grouped like this -> abcde Item, should be ignored.

I tried adding spaces to match the items in the list, but it didn't work:

.contains(' ${text} '.toUpperCase())

.contains('\n${text}\n'.toUpperCase())

List data = [{"Label": "abc Item", "Value": 10}, {"Label": "abcde Item", "Value": 20},{"Label": "edabc Item", "Value": 20}, {"Label": "item abc", "Value": 20}];

List match = [];
String text ="abc";
    
match.addAll(
  data.where(
    (oldValue) => oldValue['Label']
      .toString()
      .toUpperCase()
      .contains(text.toUpperCase()),
  ),
);
    
print(match);

expected values of the list search:

[{"Label": "abc Item", "Value": 10}, {"Label": "item abc", "Value": 20}]

CodePudding user response:

try this for individual index or dynamic list to create

List<Student> studentsDataList = [];
  
  Student std = Student();
    std.Label = "oneLabel";
    std.Value = 40;
    studentsDataList.add(std);

    std.Label = "twoLabel";
    std.Value = 80;
    studentsDataList.add(std);
  
    print(studentsDataList.length);

model class for students

class Student {
   String? Label;
   int? Value;

  Student({this.Label, this.Value,});

  Student.fromJson(Map<String, dynamic> json) {
    Label = json['Label'];
    Value = json['Value'];

  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['Label'] = this.Label;
    data['Value'] = this.Value;
    return data;
  }
}

CodePudding user response:

void main()
{
  List data = [{"Label": "abc Item", "Value": 10}, {"Label": "abcde Item", "Value": 20},{"Label": "edabc Item", "Value": 20}];
  
  List match = [];


  
  
  match.addAll(data.where((oldValue)=>oldValue["Label"]=="abc Item"));
  
  print(match);
}

This is your Ans: please try this.

  • Related