updatePollData method

void updatePollData(
  1. dynamic jsonObject,
  2. bool isStatus,
  3. bool isRepoll
)

Implementation

void updatePollData(jsonObject, bool isStatus, bool isRepoll) {
  try {

    var pollId = isStatus ? jsonObject['id'] : jsonObject['data']['id'];
    bool pollFound = false;

    _expandableDetailList.forEach((questionModel, answerModels) {

      if (questionModel.pollId == pollId.toString()) {
        pollFound = true;
        print("pollDetails:${questionModel.pollId} test $isStatus");
        if (isStatus) {
          if (isRepoll) {
            // Reinitialize the question and answer models
            List<AnswerModel> optionList = [];
            questionModel.status = "";
            questionModel.timeStamp = "";
            questionModel.pollId = jsonObject['id'];
            questionModel.questionTitle = jsonObject['question'];
            questionModel.duration = jsonObject['duration'];
            questionModel.totalPollCount = 0;

            Map<String, dynamic> options = jsonObject['options'];
            options.forEach((key, value) {
              optionList.add(AnswerModel(
                title: value,
                percentage: 0,
                numberOfPoll: 0,
                userName: "",
                userRef: "",
                clientId: "",
              ));
            });

            _expandableDetailList[questionModel] = optionList;
          } else {
            print("Pollexception: 1231");
            questionModel.status = "P";
            questionModel.timeStamp = jsonObject['timestamp'];
          }
        } else {
          questionModel.totalPollCount += 1;

          String optionSelectedValue = jsonObject['data']['optionSelectedValue'];
          for (var answerModel in answerModels) {
            if (answerModel.title == optionSelectedValue) {
              answerModel.numberOfPoll += 1;
              answerModel.userName = jsonObject['data']['name'] ?? "";
              answerModel.userRef = jsonObject['data']['user_ref'] ?? "";
              answerModel.clientId = jsonObject['data']['clientId'] ?? "";
            }
          }

          // Update percentages
          int totalPollCount = questionModel.totalPollCount;
          for (var answerModel in answerModels) {
            answerModel.percentage = (totalPollCount > 0)
                ? (answerModel.numberOfPoll * 100) ~/ totalPollCount
                : 0;


          }

          _expandableDetailList[questionModel] = answerModels;
        }
      }
    });
    print("Poll ID not found in expandableDetailList: ${_expandableDetailList.values.first.first.status}");
    if (!pollFound) {
      print("Poll ID not found in expandableDetailList: $pollId");
    }
  } catch (e) {
    print("Error updating data: $e");
  }
}