copy<T extends BaseModel> method

void copy<T extends BaseModel>(
  1. T model, {
  2. bool allowDifferentTypes = false,
  3. bool copyId = true,
  4. Iterable<String>? onlyFields,
  5. Iterable<String>? exceptFields,
})

Copies values from the given model into this model.

If allowDifferentTypes is true, the method will continue even if the types and fields in model and myself are different. Otherwise, differences will be met with a FormatException.

onlyFields and exceptFields can be used to limit the fields that are copied. The two are mutually exclusive and an error will be thrown if both are specified.

Implementation

void copy<T extends BaseModel>(
  T model, {
  bool allowDifferentTypes = false,
  bool copyId = true,
  Iterable<String>? onlyFields,
  Iterable<String>? exceptFields,
}) {
  if (!allowDifferentTypes) {
    if (type != model.type) {
      throw FormatException(
        'Types do not match! ("$type" and "${model.type}")',
      );
    }
  }
  fieldsToEvaluate(onlyFields, exceptFields)
      .where((element) {
        if (element == idKey) {
          return copyId;
        }
        return true;
      })
      .where((element) => model.fieldKeys.contains(element))
      .forEach((element) => setField(element, model.getField(element)));
}