To convert a map to an array of values in Dart, you can use the values property. This property returns an iterable of all the values in the map, which can be converted to a list.
We can convert a map to an array of values in Dart using the values property, which provides an iterable of all values in the map. We can then convert this iterable to a list.
For example,
myMap
with some key-value pairs. In this example, the map has string keys and integer values.values
property to get an iterable of the values in the map.toList
method.valuesList
.print
function to verify the conversion.void main() {
// Declare and initialize a map
Map<String, int> myMap = {
'one': 1,
'two': 2,
'three': 3
};
// Convert map to an array of values
List<int> valuesList = myMap.values.toList();
// Print the values
print('Values: $valuesList');
}
Values: [1, 2, 3]
We can also convert an empty map to an array of values in Dart using the values property, which returns an empty list when the map is empty.
For example,
emptyMap
with string keys and integer values.values
property to get an iterable of the values in the empty map.toList
method.emptyValuesList
.print
function to verify that the list is empty.void main() {
// Declare an empty map
Map<String, int> emptyMap = {};
// Convert empty map to an array of values
List<int> emptyValuesList = emptyMap.values.toList();
// Print the values
print('Values: $emptyValuesList');
}
Values: []
We can convert a map to an array of values in Dart after adding elements using the values property, which provides an updated iterable of all values in the map.
For example,
dynamicMap
with string keys and integer values.values
property to get an iterable of the values in the map after adding elements.toList
method.dynamicValuesList
.print
function to verify the updated list of values.void main() {
// Declare an empty map
Map<String, int> dynamicMap = {};
// Add elements to the map
dynamicMap['one'] = 1;
dynamicMap['two'] = 2;
dynamicMap['three'] = 3;
// Convert map to an array of values after adding elements
List<int> dynamicValuesList = dynamicMap.values.toList();
// Print the values
print('Values: $dynamicValuesList');
}
Values: [1, 2, 3]
In this tutorial, we learned How to Convert a Map to an Array of Values in Dart language with well detailed examples.