answerValue property

AnswerValue? get answerValue

The current answer. For deprecated <copy> itemsets, the selections matching the copied subtrees; for itemsets without <value>, null.

Implementation

AnswerValue? get answerValue {
  final q = question;
  final itemset = q.dynamicChoices;
  if (itemset == null) return treeElement.value;
  if (itemset.valueRef == null) return null;
  final choices = selectChoices;
  if (!itemset.copyMode) return treeElement.value;
  final destRef = itemset.destRef!.contextualize(treeElement.ref)!;
  final preselected = [
    for (final ref in form.evaluationContext.expandReference(destRef)!)
      itemset.relativeValue!.evalReadable(
        form.mainInstance,
        EvaluationContext.withContext(
          form.evaluationContext,
          form.mainInstance.resolveReference(ref)!.ref,
        ),
      ),
  ];
  final selections = [
    for (final value in preselected)
      if (choices.where((c) => c.value == value).firstOrNull case final c?)
        Selection.ofChoice(c),
  ];
  if (selections.isEmpty) return null;
  return switch (q.controlType) {
    ControlType.selectMulti ||
    ControlType.rank => MultipleItemsValue(selections),
    ControlType.selectOne => SelectOneValue(selections.first),
    _ => throw StateError("can't happen"),
  };
}