startPoll method

void startPoll(
  1. String pollId
)

Implementation

void startPoll(String pollId) {
  Map<String, dynamic> answerObject = {};
  Map<String, int> resultObject = {};
  var questionAndAnswers = getQuestionsAndAnswersByPollId(pollId);

  if (questionAndAnswers != null) {
    var question = questionAndAnswers.keys.first;
    var answers = questionAndAnswers[question];

    print('Question: ${question.questionTitle}');

    answers?.asMap().forEach((index, answer) {
      String number = (index + 1).toString();
      answerObject['opt$number'] = answer.title ?? 'Unknown Answer';  // Null safety
      resultObject['opt$number'] = 0;  // Initialize the result to 0
      print('Answer: ${answer.title}, Percentage: ${answer.percentage ?? 0}%');
    });

    // Create the main JSON object
    Map<String, dynamic> jsonObject = {
      'duration': question.duration ?? 0,  // Null safety
      'question': question.questionTitle ?? 'Unknown Question',  // Null safety
      'id': question.pollId ?? pollId,  // Fallback to pollId
      'options': answerObject,
      'result': resultObject,
      'total_result': 0,
      'initialDuration': question.duration ?? 0,  // Null safety
      'status': 'I',
    };

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

    // Send poll data
    EnxRtc.sendUserData(pollObject, true, []);

    // Update expandable list
    _expandableDetailList.forEach((questionModel, answerModels) {
      if (questionModel.pollId == question.pollId) {
        questionModel.status = "I";
      }
    });
  } else {
    print('No data found for pollId: $pollId');
  }
}