copyWith method

LightDynamics copyWith({
  1. String? status,
  2. List<String>? statusValues,
  3. double? speed,
  4. bool? speedValid,
  5. int? durationMilliseconds = -1,
  6. bool copyOriginalValues = true,
})

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

Since durationMilliseconds is nullable, it is defaulted to a negative number in this method. If left as a negative number, its current value in this LightDynamics 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 LightDynamics copy has a status that is not in its statusValues array.

Implementation

LightDynamics copyWith({
  String? status,
  List<String>? statusValues,
  double? speed,
  bool? speedValid,
  int? durationMilliseconds = -1,
  bool copyOriginalValues = true,
}) {
  // Make sure [status] is valid.
  if (statusValues != null &&
      status == null &&
      !Validators.isValidValue(this.status, statusValues)) {
    throw InvalidValueException.withValue(this.status, statusValues);
  }

  LightDynamics toReturn = LightDynamics(
    status: status ?? this.status,
    statusValues: statusValues ?? List<String>.from(this.statusValues),
    speed: copyOriginalValues ? _originalSpeed : (speed ?? this.speed),
    speedValid: speedValid ?? this.speedValid,
    durationMilliseconds: copyOriginalValues
        ? _originalDurationMilliseconds
        : (durationMilliseconds == null || durationMilliseconds >= 0
            ? durationMilliseconds
            : this.durationMilliseconds),
  );

  if (copyOriginalValues) {
    toReturn.speed = speed ?? this.speed;
    toReturn.durationMilliseconds =
        durationMilliseconds == null || durationMilliseconds >= 0
            ? durationMilliseconds
            : this.durationMilliseconds;
  }

  return toReturn;
}