如何使用dart和flutter比较现有列表和用户列表

imzjd6km  于 2023-01-02  发布在  Flutter
关注(0)|答案(2)|浏览(141)

我希望用户输入与每个列表(normalList、protanList、deutanList、tritanList)进行比较
因此,假设用户输入正确为1 - 15,则输出将为“正常”
如果用户输入为[15、14、1、2、13、12、3、4、11、10、5、6、9、8、7],则输出将为“Protan”
请帮帮忙,这是我最后一年的项目,我完全不知道该做什么
延迟列表队列框= [];//待填写
//假设用户在此处输入opaqueBox
//result方法是比较结果的结果String result(){ String result ='';如果(列表等于(正常列表,不透明框)){结果=“正常”;}否则{结果=“普罗坦”;} if(kDebugMode){打印(结果);}返回结果;}
//下面是正确的列表
变量正常列表= [“1”、“2”、“3”、“4”、“5”、“5”、“6”、“7”、“8”、“9”、“10”、“11”、“12”、“13”、“14”、“15”];变量protanList = [“15”、“14”、“1”、“2”、“13”、“12”、“3”、“4”、“11”、“10”、“5”、“6”、“9”、“8”、“7”];
变量氘列表= [“1”、“15”、“2”、“3”、“14”、“13”、“4”、“12”、“5”、“6”、“11”、“10”、“7”、“9”、“8”];
变量tritan列表= [“1”、“2”、“3”、“4”、“5”、“6”、“7”、“15”、“8”、“14”、“9”、“13”、“10”、“11”、“12”];

jq6vz3qz

jq6vz3qz1#

#把一切简单化

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

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`.

现在你可以试着找出你的方法来创造一个结果,并在这个过程中学习。祝你好运。👍

tzxcd3kk

tzxcd3kk2#

所以我已经在下面的代码中记录了我想说的话。但是这里有一个快速的。
首先,注意我认为listEquals不再受支持,您必须创建过滤条件。
这很简单也很复杂。没有办法不输入变量名或者不写一个新类就得到变量名。好吧,深入看看我做了什么。我会努力让事情变得简单和强大。
此外,在发布带有代码库的问题时,始终使用文本区域中的**“代码示例**,而不是**“Blockquote”**。
如果你需要进一步的帮助,请告诉我。

// 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
}

相关问题