Home > Blockchain >  how can i do to insert an element to an existing object in Dart
how can i do to insert an element to an existing object in Dart

Time:12-12

"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
}

CodePudding user response:

You cannot add any value to the Response itself; which you are trying to do. What you can do is store it an variable, and now you can manipulate the data.

  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}}

I created a dummy Map<String,dynamic> to replicate your fetched data, you should store it in a variable, then manipulate the data, then return it.

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

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

 // return the variable
 return responseData;
  • Related