createPoll method

void createPoll(
  1. TextEditingController etDurationController,
  2. TextEditingController etQuestionController,
  3. List<TextEditingController> etOptionsControllers
)

Implementation

void createPoll(TextEditingController etDurationController, TextEditingController etQuestionController, List<TextEditingController> etOptionsControllers){
   try {
     Map<String, dynamic> answerObject = {};
     Map<String, int> resultObject = {};
     if(etOptionsControllers.isNotEmpty){
       etOptionsControllers.asMap().forEach((index, controller) {
         print("Option ${index+1}: ${controller.text}");
         String number = (index + 1).toString();
         answerObject['opt$number'] = controller.text.trim();
         resultObject['opt$number'] = 0;
       });

     }


        // Create the main JSON object
        Map<String, dynamic> jsonObject = {
          'duration': int.parse(etDurationController.text.trim()),
          'question': etQuestionController.text.trim(),
          'id': DateTime.now().millisecondsSinceEpoch.toString(),
          'options': answerObject,
          'result': resultObject,
          'total_result': 0,
          'initialDuration': etDurationController.text.trim(),
          'status':''
        };

        // Wrap in a pollObject
        Map<String, dynamic> pollObject = {
          'type': 'POLL_START',
          'data': jsonObject,
        };

        // Log the pollObject as a string

       addData(pollObject['data']);

      } catch (e) {
        print('Error: $e');
      }



}