Hasan A Yousef
Hasan A Yousef

Reputation: 24938

DART lang, working with list of Map, how?

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;  
 }

Upvotes: 10

Views: 20189

Answers (2)

Zdeněk Mlčoch
Zdeněk Mlčoch

Reputation: 752

You can also reconsider using own class instead of map.

class OrderLine{
  String number;
  String item;
  String qty;

  OrderLine.fromElement(Element element){
    number = element.childNodes[0].value;
    /// ...
}}

for(Element element in elements){
    orderLines.add(new OrderLine.fromElement(element.childNodes[i]));
}

This solution makes your code more maintable. You can fully put typecheck to use (number will probably not be typed as String). You can use autocomplete.

Upvotes: 3

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657148

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'  : ''
             });

Upvotes: 18

Related Questions