如何检查列表是否包含 Dart 中的特定值?

Gag*_*lor 1 dictionary list nested-lists dart

如果列表包含该值,我想得到 true。目前,我粘贴在那里的这段代码返回 false。如果值存在,我希望该值应该为真。

   void main() {
  var demo = [
    {123, 1},
    {234, 1}
  ];

  print(demo.contains(123));

}
Run Code Online (Sandbox Code Playgroud)

Cha*_* Ye 7

如果您想将逻辑“传递”到列表列表,您可以尝试构建.any().every()函数。

void main() {
  var demo = [
    {123, 1},
    {234, 1}
  ];

  print(demo.any((item) => item.contains(123)));
  // output: true
}
Run Code Online (Sandbox Code Playgroud)


Jay*_*ola 5

var list = [123,11,202]; // list of int

list.contains(123); // true


var map = {'id':'123','name':'john'}; 

map.containsValue('123'); // true

var demos = [
    {123, 1},
    {234, 1}
  ];

  print(demos[0].contains(123)); //true

  **//OR**

demos.forEach((demo){
     print(demo.contains(123)); // to check all items
  });
Run Code Online (Sandbox Code Playgroud)

我希望它有帮助