[英]How can i insert new json object to existing json file (in the middle of 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.