簡體   English   中英

我該怎么做才能將元素插入 Dart 中的現有 object

[英]how can i do to insert an element to an existing object in Dart

"sale": {
   "id": 3,
   "name": "COCA"
}
 
  
 return (response.data['deliveries'] as List).map((delivery) {
      List<void> el = delivery['sale'];

      el.addAll(['price', 500]);
      
    }).toList();
 

"sale": {
    "id": 3,
    "name": "COCA",
    "price": 500
}

您不能為Response本身添加任何值; 你正在嘗試做的。 您可以做的是將它存儲為一個變量,現在您可以操作數據了。

  final dummy = <String, dynamic>{
    "sale": {"id": 3, "name": "COCA"},
  };
  print(dummy); // {sale: {id: 3, name: COCA}}

  dummy['sale']['price'] = 500;
  print(dummy); // {sale: {id: 3, name: COCA, price: 500}}

我創建了一個虛擬Map<String,dynamic>來復制您獲取的數據,您應該將它存儲在一個變量中,然后操作數據,然后返回它。

 // storing the data in a variable
 final responseData = response.data['deliveries'];

 // add the new data
 responseData['sales']['price'] = 500;

 // return the variable
 return responseData;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM