handleVotePoll function
- HandleVotePollOptions options
Handles the voting process for a poll.
The function submits a vote to the server using the specified HandleVotePollOptions.
Example:
final options = HandleVotePollOptions(
pollId: 'poll123',
optionIndex: 1,
socket: socketInstance,
showAlert: (message) => print(message),
member: 'user1',
roomName: 'roomA',
updateIsPollModalVisible: (isVisible) => setPollModalVisible(isVisible),
);
await handleVotePoll(options);
Implementation
Future<void> handleVotePoll(HandleVotePollOptions options) async {
try {
options.socket!.emitWithAck(
'votePoll',
{
'roomName': options.roomName,
'poll_id': options.pollId,
'member': options.member,
'choice': options.optionIndex
},
ack: (response) {
if (response['success']) {
options.showAlert?.call(
message: 'Vote submitted successfully',
type: 'success',
duration: 3000,
);
options.updateIsPollModalVisible(false);
} else {
options.showAlert?.call(
message: response['reason'] ?? 'Failed to submit vote',
type: 'danger',
duration: 3000,
);
}
},
);
} catch (error) {
if (kDebugMode) {
print('Error submitting vote: $error');
}
}
}