在飞镖中的多列表中找到索引?

时间:2020-09-23 03:44:12

标签: flutter dart logic

请帮助我,

我有一个带有字符串类型的多列表:

List<List<String>> mergedList = [["39", "Green"], ["40", "Blue"], ["39", "Blue"], ["40", "Green"]]

我想像这样使用indexOf来找到[“ 39”,“ Green”]的索引:

mergedList.indexOf(["39","Green"])

但是它总是返回-1。

请帮助我在哪里做错了

1 个答案:

答案 0 :(得分:0)

就您而言,您可以使用indexWhere并与element.toString()进行比较
完整代码

void main() {
  List<List<String>> mergedList = [["39", "Green"], ["40", "Blue"], ["39", "Blue"], ["40", "Green"]];
    
  var pos = mergedList.indexWhere((element) => element.toString() == ["39","Green"].toString()); 
  print(pos);
  
   var pos1 = mergedList.indexWhere((element) => element.toString() == ["39","Blue"].toString()); 
  print(pos1);
}

输出

0
2

工作演示

enter image description here