merge<T extends dynamic> static method

List<T> merge<T extends dynamic>(
  1. List<T> list,
  2. T entity,
  3. {List<String> compares = const ['id']}
)

Implementation

static List<T> merge<T extends dynamic>(List<T> list, T entity, {List<String> compares = const ['id']}){

    var present = false;
    var index = -1;
    final entityJson = entity.toJson();

    for(var t in list){

      var check = true;
      index++;
      final json = t.toJson();

      for(var compare in compares){
        if(compare.contains('.')){
          if(json[compare.split('.')[0]][compare.split('.')[1]] != entityJson[compare.split('.')[0]][compare.split('.')[1]]){
            check = false;
          }
        }
        else{
          if(json[compare] != entityJson[compare]){
            check = false;
          }
        }
      }
      if(check){ present = true; break; }
    }
    if(present){
      list[index] = entity;
    }
    else{
      list.add(entity);
    }

    return list;
}