initState method

  1. @override
void initState()
override

Called when this object is inserted into the tree.

The framework will call this method exactly once for each State object it creates.

Override this method to perform initialization that depends on the location at which this object was inserted into the tree (i.e., context) or on the widget used to configure this object (i.e., widget).

If a State's build method depends on an object that can itself change state, for example a ChangeNotifier or Stream, or some other object to which one can subscribe to receive notifications, then be sure to subscribe and unsubscribe properly in initState, didUpdateWidget, and dispose:

  • In initState, subscribe to the object.
  • In didUpdateWidget unsubscribe from the old object and subscribe to the new one if the updated widget configuration requires replacing the object.
  • In dispose, unsubscribe from the object.

You should not use BuildContext.dependOnInheritedWidgetOfExactType from this method. However, didChangeDependencies will be called immediately following this method, and BuildContext.dependOnInheritedWidgetOfExactType can be used there.

Implementations of this method should start with a call to the inherited method, as in super.initState().

Implementation

@override
void initState() {
  // Create the result object here to record the start time
  stepResult = RPStepResult(
      identifier: widget.formStep.identifier,
      questionTitle: widget.formStep.title,
      answerFormat: widget.formStep.answerFormat);
  stepResult?.questionTitle =
      "Form Step - See titles for every question included";

  // Filling up the results with nulls
  for (var item in widget.formStep.questions) {
    stepResult?.setResultForIdentifier(
        item.identifier,
        RPStepResult(
          identifier: item.identifier,
          questionTitle: item.title,
          answerFormat: item.answerFormat,
        ));
    // Set each questionTitle here in case this question can be skipped.
    (stepResult?.results[item.identifier] as RPStepResult).questionTitle =
        item.title;
  }

  readyToProceed = false;
  blocQuestion.sendReadyToProceed(false);
  recentTaskProgress = blocTask.lastProgressValue;
  timerFinished = false;

  super.initState();

  if (widget.formStep.autoSkip) {
    timeInSeconds = widget.formStep.timeout.inSeconds;
    int previousTimeInSeconds = 0;
    const oneSec = Duration(seconds: 1);
    Timer.periodic(oneSec, (t) {
      widget.formStep.timer = t;
      if (mounted) {
        setState(() {
          previousTimeInSeconds = timeInSeconds;
          timeInSeconds--;
        });
      } else {
        t.cancel();
      }
      if (timeInSeconds <= 0) {
        timerFinished = true;
        t.cancel();
        if (widget.formStep.saveResultsOnAutoSkip) {
          submitQuestionWithTempResult();
        } else {
          skipQuestion();
        }
        blocQuestion.sendReadyToProceed(true);
        t.cancel();
      }
      // For some reason, when initializing this UIFormStep, it always does it
      // twice. Thus, we need to kill the second timer if it exists.
      if (previousTimeInSeconds == timeInSeconds) {
        t.cancel();
      }
    });
  }
}