repoll method

void repoll(
  1. String pollId
)

Implementation

void repoll(String pollId) {

  try {
      // Create necessary objects
    Map<String, dynamic> dataObject = {};
    Map<String, dynamic> pollObject = {};
    Map<String, dynamic> resultObject = {};
    Map<String, dynamic> optionsObject = {};

      var questionAndAnswers = getQuestionsAndAnswersByPollId(pollId);
      var question = questionAndAnswers.keys.first;
      var answers = questionAndAnswers[question];

      // Loop through the details list
      answers?.asMap().forEach((index, answer) {
        String number = (index + 1).toString();
        optionsObject['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}%');
      });

      // Set data for poll
      dataObject["type"] = "POLL_START";
      pollObject["duration"] = question.duration;
      pollObject["question"] = question.questionTitle;
      pollObject["id"] =  DateTime.now().millisecondsSinceEpoch.toString();
      pollObject["total_result"] = 0;
      pollObject["initialDuration"] = question.duration;
      pollObject["result"] = resultObject;
      pollObject["options"] = optionsObject;
      pollObject['status']="I";

      // Set poll object to data
      dataObject["data"] = pollObject;
      EnxRtc.sendUserData(dataObject, true, []);
      // Log the data
      print("re-poll: ${jsonEncode(dataObject)}");
    addData(dataObject['data']);
    startPoll(pollObject['id']);
    //  updatePollData(dataObject['data'], true, true);

    } catch (ex) {
      print(ex);
    }

}