withPain method
Returns a new Body object with the pain details updated for a specific part. Because the class is immutable, this method is the proper way to update state.
partId: The ID of the part to update.
newPain: The new BodyPain object for the part.
Implementation
Body withPain(String partId, BodyPain newPain) {
BodyPart updateRecursive(BodyPart currentPart) {
if (currentPart.id == partId) {
return currentPart.copyWith(pain: newPain);
}
return currentPart.copyWith(
children: currentPart.children.map(updateRecursive).toList(),
);
}
return Body(parts: parts.map(updateRecursive).toList());
}