Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

flutter - 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.

question from:https://stackoverflow.com/questions/65938709/how-to-customize-array-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...