Sanket Kankariya
Sanket Kankariya

Reputation: 289

how to update data of an array inside a map in flutter

I want to Update a array inside a map

Map<int, List<String>> testtable = {
    1: ['a', 'b', 'c', 'e'],
    2: ['f', 'g', 'h', 'j'],
  };

I want to Update e => d in array which has key = 1 ,

I Have Tried this testtable.update(1, (list[3]) => d); but doesn't work

Upvotes: 0

Views: 6182

Answers (4)

Aathi
Aathi

Reputation: 3264

Update nested Map

 Map<String, Map> selectedTimes = {
    "monday": {"open": "7:30 AM", "close": "10:30 AM"},
    "tuesday": {"open": "7:30 AM", "close": "10:30 AM"},
    "wednesday": {"open": "7:30 AM", "close": "10:30 AM"},
    "thursday": {"open": "7:30 AM", "close": "10:30 AM"},
    "friday": {"open": "7:30 AM", "close": "10:30 AM"},
    "saturday": {"open": "7:30 AM", "close": "10:30 AM"},
    "sunday": {"open": "7:30 AM", "close": "10:30 AM"}
  };


selectedTimes.update(day, (list) {
  list.update(
      time, (value) => selectedTimeRTL!.format(context).toString());
  return list;
})

Upvotes: 1

Andrija
Andrija

Reputation: 1909

Another option:

void main() {
  Map<int, List<String>> testtable = {
    1: ['a', 'b', 'c', 'e'],
    2: ['f', 'g', 'h', 'j'],
  };
  
  print (testtable);
  
  testtable[1][3]='d';
  print (testtable);
}

Upvotes: 1

Vayth
Vayth

Reputation: 425

update expect the new value as the parameter, so you have to return the whole list.

testtable.update(1, (list) {
    list[3] = 'd';
    return list;
});

Upvotes: 1

Ali Alizadeh
Ali Alizadeh

Reputation: 705

You can do it this way:

testtable.update(
  1,
  (value) {
    value[3] = 'd';
    return value;
  },
);

Upvotes: 1

Related Questions