Home > OS >  Indexvalue of an element in List with Map in Dart / Flutter
Indexvalue of an element in List with Map in Dart / Flutter

Time:03-31

This is for Dart

void main() {

List nums = [{'a':1,'b':6},

               {'a':2,'b':7},
    
               {'a':3,'b':8},
    
               {'a':4,'b':9},
    
               {'a':5,'b':10}];

newValue = 4

How to print index value of element '4' In normal list we can find it as nums.indexOf('4'); how to do the same in list with key and map

Expected Output is 3 (because element 4 is at indexvalue of 3)

This is for Dart

CodePudding user response:

Using the List method indexWhere, here an example:

void main() {
  List nums = [
    {'a': 1, 'b': 6},
    {'a': 2, 'b': 7},
    {'a': 3, 'b': 8},
    {'a': 4, 'b': 9},
    {'a': 5, 'b': 10}
  ];
  
  var index = nums.indexWhere( (element) => element['a'] ==  4);
  
  print(index); // output 3
  
}

CodePudding user response:

final Map<String, int> nums = {
  "a": 1,
  "b": 2,
};

Then, your update should work:

   nums["c"] = 3;
  • Related