updateAnswer method

void updateAnswer(
  1. int jobId,
  2. String fId,
  3. dynamic answer
)

Implementation

void updateAnswer(int jobId, String fId, dynamic answer) {
  final answersForJob = jobAnswers.putIfAbsent(jobId, () => {});

  // Recursive function to find and update the answer
  bool findAndUpdateQuestion(
      List<dynamic> items, String fId, dynamic answer) {
    for (var item in items) {
      if (item is FieldGroup) {
        // If the item is a FieldGroup, search its questions
        if (findAndUpdateQuestion(item.items, fId, answer)) {
          return true; // If found, no need to continue searching
        }
      } else if (item is FieldModel && item.fId == fId) {
        // If the item is a FieldModel and the fId matches, update the answer
        item.currentAnswer = answer;
        item.evaluateDependencies(jobAnswers[jobId] ?? {});
        answersForJob[fId] = answer;
        isFormDirty = true;
        resetFieldValidationError(fId);
        return true; // Answer updated, return true
      }
    }
    return false; // fId not found in this group
  }

  // Start the recursive search for the FieldModel to update
  if (questionGroups != null &&
      !findAndUpdateQuestion(questionGroups!, fId, answer)) {
    print("No question found with fId: $fId");
  }
  notifyListeners();
}