copyValuesFrom method

void copyValuesFrom(
  1. DbModel other, {
  2. List<FieldWithValue>? fieldsToCopy,
  3. bool fullCopy = false,
})

Copy values from other db model fields to this fields.

other The other model want to copy values from.

fullCopy Copy values with (id, serverId, creationTime, modificationTime).

Implementation

void copyValuesFrom(DbModel other,
    {List<FieldWithValue>? fieldsToCopy, bool fullCopy = false}) {
  // assert(runtimeType == other.runtimeType);

  FieldWithValue? Function(String name) getCorrepondingField;
  getCorrepondingField = (String name) {
    for (var f in getAllFields()) {
      if (f != null) {
        if (f.columnName!.trim().toLowerCase() == name.trim().toLowerCase()) {
          return f;
        }
      }
    }
    return null;
  };
  List<FieldWithValue> fields = [];
  if (fieldsToCopy != null) {
    for (var f in fieldsToCopy) {
      var corrField = getCorrepondingField(f.columnName!);
      if (corrField != null) fields.add(corrField);
    }
  }
  Map<String?, FieldWithValue?> fieldsMap = {};
  for (var e in (fullCopy
      ? getAllFields()
      : fieldsToCopy == null
          ? getFields()!
          : fields)) {
    fieldsMap[e?.columnName] = e;
  }

  Map<String, FieldWithValue> otherFieldsMap = {};
  var otherFields = (fullCopy
      ? other.getAllFields()
      : fieldsToCopy == null
          ? other.getFields()
          : other._getNamedFields(
              fieldsToCopy.map((e) => e.columnName).toList()));
  otherFields?.forEach((e) => otherFieldsMap[e!.columnName!] = e);

//Copy values
  for (var k in fieldsMap.keys) {
    var thisField = fieldsMap[k];
    var otherField = otherFieldsMap[k];

    if (thisField != null && otherField != null) {
      thisField.value = otherField.value;
    }
  }
}