stopPoll method

void stopPoll(
  1. String pollId
)

Implementation

void stopPoll(String pollId) {
  try {
    Map<String, dynamic> dataObject = {};
    Map<String, dynamic> pollObject = {};
    Map<String, dynamic> pollNObject = {};
    var questionAndAnswers = getQuestionsAndAnswersByPollId(pollId);
    var question = questionAndAnswers.keys.first;
    var answers = questionAndAnswers[question];

    this.pollId = question.pollId;

    pollNObject['type'] = 'MCQ1';
    pollNObject['question'] = question.questionTitle;
    pollNObject['status'] = 'P';
    pollNObject['total_result'] = question.totalPollCount;
    pollNObject['id'] = question.pollConst;
    pollNObject['conf_num'] = confNumber.value;
    pollNObject['duration'] = question.duration;
    pollNObject['timestamp'] =
        DateTime.now().millisecondsSinceEpoch.toString();

    Map<String, dynamic> resultObject = {};
    Map<String, dynamic> optionsObject = {};

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

    pollObject[question.pollId] = pollNObject;
    dataObject['polls'] = pollObject;
    pollNObject['options'] = optionsObject;
    pollNObject['result'] = resultObject;

    List<Map<String, dynamic>> resultArray = [];

    answers?.asMap().forEach((index, answer) {
      Map<String, dynamic> resultJson = {};
      resultJson['name'] = answer.title;
      resultJson['percentage'] = answer.percentage;
      resultArray.add(resultJson);
    });
    pollJsonObject['data'] = dataObject;
    Map<String, dynamic> data = {};
    data['id'] = int.parse(pollId);
    Map<String, dynamic> publishPoll = {
      'type': 'poll-stop',
      'data': data,
    };

    EnxRtc.sendUserData(publishPoll, true, []);

    Map<String, dynamic> dataOption = {
      'scope': EnxCustomDataScope.room.name,
      'query': 'polls.$pollId',
    };
    print("TestData ${jsonEncode(pollJsonObject)}");

    EnxRtc.saveCustomData(
        dataOption, pollJsonObject["data"]["polls"][pollId]);
  } catch (e) {
    print(e);
  }
}