[flutter] Map<K,V> add, update, delete code example

0

Add Code

Map<String, BoraModel> _map;
_map = _map == null
          ? {updateKey: model}
          : {..._notifications, updateKey: model};

Update Code

Map<String, BoraModel> _map;
_map.update(
        updateKey,
        (beforeVal) => model,
      );

Delete Code

Map<String, BoraModel> _map;
_map.removeWhere((key, value) => key == updateKey);


πŸ˜‰Example

model (VO)

class BoraModel {
  final String x;
  final String y;

  BoraModel(
      {this.x,
      this.y});
}
enum Action {
  Add,
  Update,
  Delete,
}

add, update, delete

Map<String, BoraModel> _map;

void _cudNotiMap(int actionIdx, String updateKey, BoraModel model) {
    if (actionIdx == Action.Add.index) {
      _map = _map == null
          ? {updateKey: model}
          : {..._notifications, updateKey: model};
    }
    if (actionIdx == Action.Update.index) {
      _map.update(
        updateKey,
        (beforeVal) => model,
      );
    }
    if (actionIdx == Action.Delete.index) {
      _map.removeWhere((key, value) => key == updateKey);
    }
  }
profile
𝙸 πšŠπš– 𝚊 πšŒπšžπš›πš’πš˜πšžπšœ πšπšŽπšŸπšŽπš•πš˜πš™πšŽπš› πš πš‘πš˜ πšŽπš—πš“πš˜πš’πšœ πšπšŽπšπš’πš—πš’πš—πš 𝚊 πš™πš›πš˜πš‹πš•πšŽπš–. πŸ‡°πŸ‡·πŸ‘©πŸ»β€πŸ’»

0개의 λŒ“κΈ€