繁体   English   中英

如何将现有列表与用户列表进行比较? 使用 dart 和 flutter

[英]How to compare existing list with list from user? using dart and flutter

我希望用户输入与每个列表(normalList、protanList、deutanList、tritanList)进行比较

因此,假设如果用户输入正确为 1 - 15,则 output 将为“正常”

如果用户输入 [15,14,1,2,13,12,3,4,11,10,5,6,9,8,7],output 将是“Protan”

请帮忙,这是我最后一年的项目,我完全不知道该怎么做

后期列表<Box> opaqueBoxes = []; //待填充

//假设用户在这里输入不透明框

//result方法是比较result的结果 String result() { String result = ''; if (listEquals(normalList, opaqueBoxes)) { result = "Normal"; } else { 结果 = "Protan"; } 如果(kDebugMode){ 打印(结果); } 返回结果; }

//这里是正确的列表

var normalList = [ "1", "2", "3", "4", "5", "5", "6", "7", "8", "9", "10", "11 ", "12", "13", "14", "15" ]; var protanList = [ "15", "14", "1", "2", "13", "12", "3", "4", "11", "10", "5", "6 ", "9", "8", "7" ];

var deutanList = [ "1", "15", "2", "3", "14", "13", "4", "12", "5", "6", "11", "10 ", "7", "9", "8" ];

var tritanList = [ "1", "2", "3", "4", "5", "6", "7", "15", "8", "14", "9", "13 ", "10", "11", "12" ];

所以我已经在下面的代码中记录了我想说的话。 但这是一个快速的。

首先,请注意我认为listEquals不再受支持。 您将必须创建过滤条件。

这很简单也很复杂。 如果不输入变量名或编写新的 class,就无法获得变量名。好吧,深入了解一下我做了什么。 我会努力让事情变得简单而强大。

此外,在使用代码库发布问题时,请始终在文本区域中使用“代码示例”而不是“块引用”

如果您需要进一步的帮助,请告诉我。

// First, note that I don't think `listEquals` is in support anymore. You will have
// create your filter condition.
// This is easy and complex. There is no way to get a variable name without
// typing them or writing a new class writing a new function. Well, deep in 
// and see what I did. I will try and make Things easy and strong.

void main() {
  //   I changed to type `Box` to `String` as we are getting a string input
  late List<String> opaqueBoxes = [
    "1",
    "15",
    "2",
    "3",
    "14",
    "13",
    "4",
    "12",
    "5",
    "6",
    "11",
    "10",
    "7",
    "9",
    "8"
  ];

  // Lists to filter against. Added type casting
  final List<String> normalList = [
    "1",
    "2",
    "3",
    "4",
    "5",
    "5",
    "6",
    "7",
    "8",
    "9",
    "10",
    "11",
    "12",
    "13",
    "14",
    "15"
  ];
  final List<String> protanList = [
    "15",
    "14",
    "1",
    "2",
    "13",
    "12",
    "3",
    "4",
    "11",
    "10",
    "5",
    "6",
    "9",
    "8",
    "7"
  ];
  final List<String> deutanList = [
    "1",
    "15",
    "2",
    "3",
    "14",
    "13",
    "4",
    "12",
    "5",
    "6",
    "11",
    "10",
    "7",
    "9",
    "8"
  ];
  final List<String> tritanList = [
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "15",
    "8",
    "14",
    "9",
    "13",
    "10",
    "11",
    "12"
  ];

  // transform your list-based filter to list of objects based for easy filtering
  final List allFilterList = [
    {"name": "normal", "list": normalList},
    {"name": "protan", "list": protanList},
    {"name": "deutan", "list": deutanList},
    {"name": "tritan", "list": tritanList},
  ];

  // result output
  String result = "No such list!";

  for (Map list in allFilterList) {
    // filter using `any` Function from dart `List` class
    bool filter = opaqueBoxes.any((item) => list["list"].contains(item));
    if (filter) result = list["name"];
  }

  print("result: $result"); // result: tritan
}

#TO把一切简单

如果您需要两个列表都保持相同的顺序,请尝试以下方式:

bool isSame = a.every((item) => item == b[a.indexOf(item)]);
//List `a` is getting compared with list `b`.

如果您不需要以完全相同的顺序排列这些项目,您可以试试这个:

bool isSame = a.every((item) => b.contains(item));
//List `a` is getting compared with list `b`.

现在您可以尝试找出从中创建结果的方法。 并在这个过程中学习。 祝你好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM