rameez khan
rameez khan

Reputation: 359

How to customize array values

I am fetching data from a SQLite database. What I need to do is put data in one array. Right now, I'm doing this in my foreach loop. I'm able to to put data in one array, but issue is I need to customize the array.

This is my code

var allRows = await dbHelper.queryAllRows();

allRows.forEach((row) {
  items.add(row);
  amount += double.parse(row['price']);
  print(amount);
  print(row);
  print(items);
});

You can see I am simply putting row in Items array, but I need to customize it like this

"OrderDetails": [
    {
        "ItemID": row['id'],
        "ItemName": row['title'],
        "Quantity": 1,
        "Price": 12,
        "OrderDetailModifiers": [
            {
                "ModifierName": row['sizeselect']",

            },
            {
                "ModifierName": row['color']",

            }
        ]
    }
]

This is the structure. Basically, it's a body of postman API I need to post data like this. So I need to customize the Items array, right now I'm simply putting all rows in Items array.

Upvotes: 0

Views: 32

Answers (1)

Umaiz Khan
Umaiz Khan

Reputation: 1227

You can simple use your foreach loop and add items in this.

allRows.forEach((row) {
  items.add(
    {
      {
        "ItemID": row['price'],
        "ItemName": row['price'],
        "Quantity": 1,
        "Price": row['price'],
        "Cost": 0,
        "StatusID": 1,
        "OrderDetailModifiers": [
          {
            "ModifierID": 0,
            "ModifierName": row['price'],
            "Quantity": 0,
            "Price": 0,
            "Cost": 0,
            "StatusID": 1
          },
          {
            "ModifierID": 0,
            "ModifierName": row['price'],
            "Quantity": 0,
            "Price": 0,
            "Cost": 0,
            "StatusID": 1
          }
        ]
      }
    }
  );
  amount += double.parse(row['price']);
  print(amount);
  print(row);
  print(items);
});

Sorry I have added row['price'] at some places you can replace all of them by your need

Upvotes: 1

Related Questions