Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DART lang, working with list of Map, how?

Tags:

dart

I'e a sales order of many lines, want to handle each line as a Map of (number, Item code, Qty), and need to have all these maps in list, so that orderlines[0] is the Map of the first order line.

If I've 3 lines, then the below worked very well.

But what if I do not know the number of lines, and it depend on the user, then how can define it!

  var orderLines = [{ 'number'  : '',
                  'Item' : '',
                  'Qty'  : ''
                 },
                 { 'number'  : '',
                  'Item' : '',
                  'Qty'  : ''
                 },
                 { 'number'  : '',
                  'Item' : '',
                  'Qty'  : ''
                 }
               ];

I need to define the "orderLines" so I can get it filled by the data in the below code:

   for(int i=0;i<=number_of_lines;i++){
       orderLines[i]['number']=element.childNodes[i].childNodes[0].value;
       orderLines[i]['number']=element.childNodes[i].childNodes[1].value;
       orderLines[i]['Qty']=element.childNodes[i].childNodes[2].value;  
 }
like image 499
Hasan A Yousef Avatar asked Sep 06 '14 13:09

Hasan A Yousef


People also ask

What does list map () do in Dart?

Returns a new lazy Iterable with elements that are created by calling f on each element of this Iterable in iteration order. This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked.

What is difference between list and map in Dart?

Lists:: A list is an ordered collection of elements that are distinguished by their indices. List elements can be of any data type—primitive types, collections, sObjects, user-defined types, and built-in Apex types. A map is a collection of key-value pairs where each unique key maps to a single value.


1 Answers

Update according to the updated question

You can't assign a value to a List at a specific index if the list is shorter than the index. You have to fill the list using add. If the list already has a value at a given index you can replace the value by mylist[someIndex] = someValue;

var orderLines = <Map>[]; // creates an empty List<Map>

for(int i=0; i <= number_of_lines; i++){
  var map = {};
  map['number'] = element.childNodes[i].childNodes[0].value;
  map['Item'] = element.childNodes[i].childNodes[1].value;
  map['Qty'] = element.childNodes[i].childNodes[2].value; 
  orderLines.add(map);
}

or

for(int i = 0; i <= number_of_lines; i++){
  orderLines.add({
    'number': element.childNodes[i].childNodes[0].value,
    'Item': element.childNodes[i].childNodes[1].value,
    'Qty': element.childNodes[i].childNodes[2].value});
}

Original answer

I'm not sure what you want to know but I suppose this is what you are looking for

var orderLines = <Map>[]; // creates an empty List<Map>

orderLines.add({ 'number'  : '',
              'Item' : '',
              'Qty'  : ''
             });
orderLines.add({ 'number'  : '',
              'Item' : '',
              'Qty'  : ''
             });
orderLines.add({ 'number'  : '',
              'Item' : '',
              'Qty'  : ''
             });
like image 128
Günter Zöchbauer Avatar answered Sep 19 '22 17:09

Günter Zöchbauer