0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.