withVoteApplied method
Returns a copy with previousVote's counts removed and newVote's counts
added, so a host app can optimistically update vote counts locally after
a PollVoteChanged callback fires.
Implementation
Poll<T> withVoteApplied({
required PollVote<T>? previousVote,
required PollVote<T>? newVote,
}) {
final previousIds = previousVote?.selectedOptionIds ?? const {};
final newIds = newVote?.selectedOptionIds ?? const {};
return copyWith(
options: options.map((option) {
var voteCount = option.voteCount;
if (previousIds.contains(option.id) && !newIds.contains(option.id)) {
voteCount -= 1;
} else if (!previousIds.contains(option.id) &&
newIds.contains(option.id)) {
voteCount += 1;
}
return option.copyWith(voteCount: voteCount);
}).toList(),
);
}