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() {
  super.initState();
  // Instantiate the task result so it starts tracking time
  _taskResult = RPTaskResult(identifier: widget.task.identifier);

  // If it's navigable we don't want to show result on app bar
  if (widget.task is RPNavigableOrderedTask) {
    blocTask.updateTaskProgress(RPTaskProgress(0, widget.task.steps.length));
    navigableTask = true;
  } else {
    // Sending the initial Task Progress so the Question UI can use it in the app bar
    blocTask.updateTaskProgress(RPTaskProgress(
        _currentQuestionIndex, widget.task.numberOfQuestionSteps));
  }

  // Subscribe to step status changes so the navigation can be triggered
  _stepStatusSubscription = blocTask.stepStatus.listen((status) async {
    switch (status) {
      case RPStepStatus.Finished:
      case RPStepStatus.Skipped:
        // For now, the same thing has to happen whether a step is finished or skipped.
        // But the "Skipped" status is included to cover this case also.

        // In case of last step we save the result and close the task
        if (_currentStep == widget.task.steps.last) {
          createAndSendResult();
          if (widget.task.closeAfterFinished) {
            Navigator.of(context).pop();
          }
          break;
        }
        _currentStep?.timer?.cancel();
        // Updating taskProgress stream
        if (_currentStep.runtimeType == RPQuestionStep) {
          _currentQuestionIndex++;
          if (!navigableTask) {
            blocTask.updateTaskProgress(RPTaskProgress(
                _currentQuestionIndex, widget.task.numberOfQuestionSteps));
          }
        }

        // Find the next step and then navigate there
        setState(() {
          _currentStep = _activeSteps.last;
          _currentStep = widget.task.getStepAfterStep(_currentStep, null);
          if (_currentStep != null) _activeSteps.add(_currentStep!);
        });
        _currentStepIndex++;

        _taskPageViewController.nextPage(
            duration: const Duration(milliseconds: 400),
            curve: Curves.easeInOut);
        break;
      case RPStepStatus.Canceled:
        showCancelConfirmationDialog();
        break;
      case RPStepStatus.Back:
        // If the stepWidgets list only has 1 element it means the user is on
        // the first question, so no back navigation is enabled.
        if (_activeSteps.length == 1) {
          break;
        }
        if (_currentStep == widget.task.steps.first) {
          break;
        } else {
          _currentQuestionIndex--;
          _currentStepIndex--;
          if (!navigableTask) {
            blocTask.updateTaskProgress(RPTaskProgress(
                _currentQuestionIndex, widget.task.numberOfQuestionSteps));
          }
          // await because it can only update the stepWidgets list while the current step is out of the screen
          await _taskPageViewController.previousPage(
              duration: const Duration(milliseconds: 400),
              curve: Curves.easeInOut);

          setState(() {
            _activeSteps.removeLast();
            _currentStep = _activeSteps.last;
          });
        }
        blocQuestion.sendReadyToProceed(true);
        break;
      case RPStepStatus.Ongoing:
        break;
      default:
        break;
    }
  });

  _stepResultSubscription = blocTask.stepResult.listen((stepResult) {
    if (_taskResult != null) {
      _taskResult?.setStepResultForIdentifier(
          stepResult.identifier, stepResult);
      blocTask.updateTaskResult(_taskResult!);
    }
  });

  setState(() {
    // Getting the first step
    _currentStep = widget.task.getStepAfterStep(null, null);
    if (_currentStep != null) _activeSteps.add(_currentStep!);
  });
}