Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart Map : get the first key only

Tags:

dart

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);
like image 78
Ουιλιαμ Αρκευα Avatar answered Mar 22 '26 14:03

Ουιλιαμ Αρκευα


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
}
like image 28
Ahmed Khattab Avatar answered Mar 22 '26 15:03

Ahmed Khattab