gpt4 book ai didi

flutter - 在dart中执行搜索操作时如何忽略类中的某些变量

转载 作者:行者123 更新时间:2023-12-03 03:34:57 25 4
gpt4 key购买 nike

假设我在Dart中有一个类(class)

class Student{
String name;
int age;
String grade;
}

和它一起,我有一个学生列表,如下
List<Student> students = [
Student('Sam', 18, 'A'),
Student('Mark', 20, 'B+'),
Student('Moses', 18, 'A-'),
Student('Ron', 18, 'B'),
];

我想执行搜索,以便忽略该名称。
students.where(Student(IGNORE,18,'A'));

有什么办法可以做到这一点?

最佳答案

您可以为匿名函数中的.where方法分配 bool(boolean) 谓词,而不必搜索要忽略的字段。

List<Student> students = [
Student('Sam', 18, 'A'),
Student('Mark', 20, 'B+'),
Student('Moses', 18, 'A-'),
Student('Ron', 18, 'B'),
];

students.where((student) => (student.age==18 && student.grade=="A")); // Iterable containing Instance named Sam.

不论名称如何,都将返回带有“A”和18yo的所有“'学生”'实例的可迭代对象。
This question提供了有关如何使用 .where方法进行过滤的更深入的说明。

关于flutter - 在dart中执行搜索操作时如何忽略类中的某些变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61719154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com