copyWith method

LightAlert copyWith({
  1. List<String>? actionValues,
  2. String? action = "",
  3. bool copyOriginalValues = true,
})

Returns a copy of this object with its field values replaced by the ones provided to this method.

Since action is nullable, it is defaulted to an empty string in this method. If left as an empty string, its current value in this LightAlert object will be used. This way, if it is null, the program will know that it is intentionally being set to null.

copyOriginalValues is true if you want to maintain the original object's initial values. This is useful if you plan on using this object in a PUT request.

Throws InvalidValueException if the LightAlert copy has an action that is not in its actionValues array.

Implementation

LightAlert copyWith({
  List<String>? actionValues,
  String? action = "",
  bool copyOriginalValues = true,
}) {
  // Make sure [actionValue] is valid.
  if (actionValues != null &&
      (action != null && action.isEmpty) &&
      !Validators.isValidValue(this.action, actionValues)) {
    throw InvalidValueException.withValue(this.action, actionValues);
  }

  LightAlert toReturn = LightAlert(
    actionValues: actionValues ?? List<String>.from(this.actionValues),
    action: copyOriginalValues
        ? _originalAction
        : (action == null || action.isNotEmpty ? action : this.action),
  );

  if (copyOriginalValues) {
    toReturn.action =
        action == null || action.isNotEmpty ? action : this.action;
  }

  return toReturn;
}