I have customer collection inside I have list of merchant data like
{
'customer':'xyz',
'contactNumber':'999999999',
'merchantMap':[
{
'merchantName':'abx',
'merchantId':'dsdfsbdmmm'
},
{'merchantName':'abx',
'merchantId':'dsdfsbdmmm'
}
]
}
Here inside merchantMap I want to update his name by checking his merchantId. How can I achieve this?
CodePudding user response:
Lets assume your customer data is this:
var customerData = {
'customer': 'xyz',
'contactNumber': '999999999',
'merchantMap': [
{'merchantName': 'abx', 'merchantId': '12'},
{'merchantName': 'abx', 'merchantId': '23'}
]
};
and your specific id is this:
String selectedId = "23";
you can do this to change the item that contains that id:
var newMerchantMap =
(customerData["merchantMap"] as List<Map<String, dynamic>>)
.map((e) => e['merchantId'] == selectedId
? {'merchantName': 'newNAme', 'merchantId': e["merchantId"]}
: e)
.toList();
customerData['merchantMap'] = newMerchantMap;
result :
{
customer: xyz,
contactNumber: 999999999,
merchantMap: [
{merchantName: abx, merchantId: 12},
{merchantName: newNAme, merchantId: 23}
]
}