withVoteApplied method

Poll<T> withVoteApplied({
  1. required PollVote<T>? previousVote,
  2. required PollVote<T>? newVote,
})

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: [
      for (final option in options)
        option.copyWith(
          voteCount:
              option.voteCount +
              (newIds.contains(option.id) ? 1 : 0) -
              (previousIds.contains(option.id) ? 1 : 0),
        ),
    ],
  );
}