Dart Map : get the first key only

Viewed 3269
void main() { 
   var details = {'LastName':'Martin','FirstName':'Pierre'}; 
   print(details.keys); 
}

It will produce the following output : (LastName, FirstName)

But How can I print "LastName" only ?

2 Answers
print(details.keys.toList().first);

As defined in dart docs the method Map.keys returns an Iterable object, that is in iteself an iterable (List) like object. You can access the first element of an iterable with the first getter method. And you can use last to access the last element of an Iterable

void main() { 
   var details = {'LastName':'Martin','FirstName':'Pierre'}; 
   print(details.keys.first);  //LastName
   print(details.keys.last);  //FirstName
}
Related