Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter How To Update/Add Value to a Key in a Map<K,V>

Tags:

flutter

dart

Currently I have a map that looks like this to send emails via an API:

Map body = {"personalizations": [
    {
      "to": [
        {
          "email": "$receiverEmail"
        }
      ],
      "dynamic_template_data": {
        "EmployeeName": "$employeeName",
        "EmployeeID": "$employeeID",
        "PatientName": "$patientName",
        "ProviderName": "$providerName",
        "TreatmentDate": "$treatmentDate",
        "Diagnosis": "$diagnosis"
      }
    }
  ],
  "from": {
    "email": "$userEmail"
  },
  "template_id": "$templateID"
};

I am planning to use this structure with 2 forms of emails and for that to happen I need to update/add values under the dynamic_template_data key.

Therefore I am trying to find out how I could update/add value to that specific key. I found a function called Map.update() but I am unsure as how to properly use it. How do I approach this problem?

like image 692
Asyraf Dayan Avatar asked Sep 03 '25 13:09

Asyraf Dayan


1 Answers

Just assign a new value to a specific key to update.

body['personalizations'][0]['dynamic_template_data']['EmployeeName'] = 'John Doe';

or

body['personalizations'][0]['dynamic_template_data']['Salary'] = 5000.00;

another example to do an assignment only if it doesn't exist yet

(body['personalizations'][0] as Map).putIfAbsent('Salary', () => 5000.00);
like image 131
Günter Zöchbauer Avatar answered Sep 05 '25 03:09

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!