handleCreatePoll function
- HandleCreatePollOptions options
Handles the creation of a poll by emitting a "createPoll" event with the provided details. Shows an alert based on the success or failure of the operation.
Example:
final options = HandleCreatePollOptions(
poll: {'question': 'Favorite color?', 'type': 'singleChoice', 'options': ['Red', 'Blue', 'Green']},
socket: socketInstance,
roomName: 'roomA',
showAlert: (message) => print(message),
updateIsPollModalVisible: (isVisible) => setIsPollModalVisible(isVisible),
);
await handleCreatePoll(options);
Implementation
Future<void> handleCreatePoll(HandleCreatePollOptions options) async {
try {
var pollMap = options.poll.toMap();
// keep only the question, type, and options
pollMap.removeWhere(
(key, value) => key != 'question' && key != 'type' && key != 'options');
options.socket!.emitWithAck(
'createPoll',
{'roomName': options.roomName, 'poll': pollMap},
ack: (response) {
if (response['success']) {
options.showAlert?.call(
message: 'Poll created successfully',
type: 'success',
duration: 3000,
);
options.updateIsPollModalVisible(false);
} else {
options.showAlert?.call(
message: response['reason'] ?? 'Failed to create poll',
type: 'danger',
duration: 3000,
);
}
},
);
} catch (error) {
if (kDebugMode) {
print('Error creating poll: $error');
}
}
}