简体   繁体   English

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

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

I want the user input to compare with each list (normalList, protanList,deutanList, tritanList)我希望用户输入与每个列表(normalList、protanList、deutanList、tritanList)进行比较

so, assuming if the the user input is 1 - 15 correctly, and the output will be "Normal"因此,假设如果用户输入正确为 1 - 15,则 output 将为“正常”

if the user input is [15,14,1,2,13,12,3,4,11,10,5,6,9,8,7],the output will be "Protan"如果用户输入 [15,14,1,2,13,12,3,4,11,10,5,6,9,8,7],output 将是“Protan”

Please help, this is my final year project and I'm totally blank about what to do请帮忙,这是我最后一年的项目,我完全不知道该怎么做

late List<Box> opaqueBoxes = [];后期列表<Box> opaqueBoxes = []; //to be filled //待填充

//assume opaqueBoxes being input by user here //假设用户在这里输入不透明框

//the result method is to compare the outcome of the result String result() { String result = ''; //result方法是比较result的结果 String result() { String result = ''; if (listEquals(normalList, opaqueBoxes)) { result = "Normal"; if (listEquals(normalList, opaqueBoxes)) { result = "Normal"; } else { result = "Protan"; } else { 结果 = "Protan"; } if (kDebugMode) { print(result); } 如果(kDebugMode){ 打印(结果); } return result; } 返回结果; } }

//here is the list of the correct one //这里是正确的列表

var normalList = [ "1", "2", "3", "4", "5", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15" ]; 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 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 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" ]; var tritanList = [ "1", "2", "3", "4", "5", "6", "7", "15", "8", "14", "9", "13 ", "10", "11", "12" ];

So I already documented what I wanted to say in the code below.所以我已经在下面的代码中记录了我想说的话。 But here is a quick one.但这是一个快速的。

First, note that I don't think listEquals is in support anymore.首先,请注意我认为listEquals不再受支持。 You will have to 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. Well, deep in and see what I did.如果不输入变量名或编写新的 class,就无法获得变量名。好吧,深入了解一下我做了什么。 I will try and make things easy and strong.我会努力让事情变得简单而强大。

Also when posting a question with the codebase, always use the "Code Sample not the "Blockquote" on your textarea.此外,在使用代码库发布问题时,请始终在文本区域中使用“代码示例”而不是“块引用”

If you need further assistance let me know.如果您需要进一步的帮助,请告诉我。

// 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 PUT EVERYTHING SIMPLY #TO把一切简单

If you need both lists to maintain the same sequence, then try this way:如果您需要两个列表都保持相同的顺序,请尝试以下方式:

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

And if you don't need to have the items in exact same sequence you can try this:如果您不需要以完全相同的顺序排列这些项目,您可以试试这个:

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

Now you can try and figure out your ways to create a result from it.现在您可以尝试找出从中创建结果的方法。 And learn in the process.并在这个过程中学习。 Good luck.祝你好运。

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

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