Home computickets How to get keys and values ​​by index from Map in Dart?

How to get keys and values ​​by index from Map in Dart?




I want to get keys and value in the index, but I can not understand how it can be realized watched solutions in other languages ​​did not understand anything, I will be deeply grateful if you can help?

Answer 1, Authority 100%

Map Dart is Key-Value Pair – where each element has a key and value. Thanks to the properties of Key and Value , which have this collection, we can get the key and value of the element. But in order to work efficient, I propose to use another type of collection, namely list & lt; T & GT; . It has much more opportunities unlike MAP , but also there are disadvantages: Map Much quickly processes in a small collection than List , but the more the collection, the slower begins to be processed Map .

@ Mikhail Rebrov, already gave the answer as you can solve your question. I, in turn, show an example of how to use a bundle Map and list . (Naturally dynamic is used for example, and it is recommended to make strict typing)

void main () {
 // Create List with our structure
 List & lt; ListStructure & gt; List = New List & LT; ListStructure & gt; ();
 // Create Map and fill List values ​​of MAP
 Map & Lt; Dynamic, Dynamic & GT; Map = {
    "1": "Tom",
    "2": "bob",
    "3": "SAM"
  } .. foreach ((k, v) = & gt; list.add (new liststructure (k, v)));
 // withdraw the LIST element and index)
 For (int i = 0; i & lt; list.length; i ++) {
  Print ("$ {List [i] .Key}: $ {list [i] .values}");
 Print ("");
 // Add a new element to List
 List.Add (NEW Liststructure ("4", "Kail"));
 // Assign Map List Values
 Map = List.asmap ();
 // withdraw Map element and index)
 For (int i = 0; i & lt; map.length; I ++) {
  Print ("$ {Map [i] .Key}: $ {map [i] .values}");
// Structure List.
Class ListStructure {
 Dynamic Key;
 Dynamic Values;
 ListStructure (this.key, this.Values);


1: Tom
2: Bob
3: Sam.
1: TOM.
2: Bob
3: Sam.
4: Kail.

Answer 2, Authority 50%

Suppose We have Map & LT; K, V & GT; , where
K is the type of key
And V is the type of value.

What can we do?
At the copy map & lt; k, v & gt; , there are properties:

  • Keys Store Iterable & Lt; K & GT; , in which the keys are
  • Values ​​Store ITERABLE & LT; V & GT; , in which

What is more remarkable, instance map & lt; k, v & gt; , there is a entries property, which stores ITERABLE & LT; MAPENTRY & LT; K, V & GT; & GT ; , in which there are MAP .
The supervisory reader, of course, will notice that I am talking about ITERABLE , which of course you can learn, but you cannot get a value in the index.
Of course, it is so, but an instance of an abstract class ITERABLE , there is a tolist () method, which brings this instance to the usual list, from which you can already get an element by index using Elementat () method.


main (list & lt; string & gt; arguments) {
 var Map = Map & lt; String, String & GT; ();
 Map = {
  'one': '1',
  'Two': '2',
  'Three': '3'
 // Receive and save in a separate variable list of occurrences.
 var entries = map.tentries.tolist ();
 Print ('output data received from the list of entries:');
 Print (entries.elementat (0 )Key + ':' + entries.Elementat (0) .Value);
 Print (entries.elementat (1) .Key + ':' + entries.Elementat (1) .Value);
 Print (entries.elementat (2) .Key + ':' + entries.elementat (2) .Value);
 // Receive and save in separate key list and list of values.
 var keys = map.keys.tolist ();
 var values ​​= map.values.tolist (); 
Print ('Output of data received separately from the key list and from the list of values:');
  Print (Keys.Elementat (0) + ':' + Values.Elementat (0));
  Print (Keys.Elementat (1) + ':' + Values.Elementat (1));
  Print (Keys.Elementat (2) + ':' + Values.Elementat (2));


output of data received from the list of occurrences:
One: 1.
Two: 2.
Three: 3.
Displays the data received separately from the key list and from the list of values:
One: 1.
Two: 2.
Three: 3.

It seems like everything correctly.
Use on health.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions