toJson method
Converts this object into JSON format.
optimizeFor
lets the program know what information to include in the
JSON data map.
- OptimizeFor.put (the default value) is used when making a data map that is being placed in a PUT request. This only includes data that has changed.
- OptimizeFor.putFull is used when a parent object updates; so, all of the children are required to be present for the PUT request.
- OptimizeFor.post is used when making a data map for a POST request.
- OptimizeFor.dontOptimize is used to get all of the data contained in this object.
Throws InvalidValueException if effect is empty and optimizeFor
is
not set to OptimizeFor.dontOptimize.
Implementation
Map<String, dynamic> toJson({OptimizeFor optimizeFor = OptimizeFor.put}) {
// Validate [effect].
if (!identical(optimizeFor, OptimizeFor.dontOptimize)) {
if (!Validators.isValidValue(effect, effectValues)) {
if (!identical(optimizeFor, OptimizeFor.put) ||
effect != _originalEffect) {
throw InvalidValueException.withValue(effect, effectValues);
}
}
}
// PUT
if (identical(optimizeFor, OptimizeFor.put)) {
Map<String, dynamic> toReturn = {};
if (effect != _originalEffect) {
toReturn[ApiFields.effect] = effect;
}
if (duration != _originalDuration) {
toReturn[ApiFields.duration] = duration;
}
return toReturn;
}
// PUT FULL
if (identical(optimizeFor, OptimizeFor.putFull)) {
return {
ApiFields.effect: effect,
ApiFields.duration: duration,
};
}
// DEFAULT
return {
ApiFields.effect: effect,
ApiFields.duration: duration,
ApiFields.statusValues: statusValues,
ApiFields.status: status,
ApiFields.effectValues: effectValues,
};
}