Home > OS >  How to compare existing list with list from user? using dart and flutter
How to compare existing list with list from user? using dart and flutter

Time:01-01

I want the user input to compare with each list (normalList, protanList,deutanList, tritanList)

so, assuming if the the user input is 1 - 15 correctly, and the output will be "Normal"

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"

Please help, this is my final year project and I'm totally blank about what to do

late List<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 = ''; if (listEquals(normalList, opaqueBoxes)) { result = "Normal"; } else { result = "Protan"; } if (kDebugMode) { print(result); } 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 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" ];

CodePudding user response:

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

CodePudding user response:

#TO PUT EVERYTHING SIMPLY

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.

  • Related