2

I have a problem with a function. I need to realize a func which will be work correctly when user taps on button. I need to froze this func and wait for user's action. (tap on button). Maybe y have already to faced to the same prob

  void submitAnswer(String submittedAnswer) async {
    timerAnimationController.stop();
    if (!context.read<QuestionsCubit>().questions()[currentQuestionIndex].attempted) {
      context.read<QuestionsCubit>().updateQuestionWithAnswerAndLifeline(context.read<QuestionsCubit>().questions()[currentQuestionIndex].id, submittedAnswer);
      updateTotalSecondsToCompleteQuiz();
      //change question
      await Future.delayed(Duration(seconds: inBetweenQuestionTimeInSeconds));
      if (currentQuestionIndex != (context.read<QuestionsCubit>().questions().length - 1)) {
        updateSubmittedAnswerForBookmark(context.read<QuestionsCubit>().questions()[currentQuestionIndex]);
        timerAnimationController.stop();

        //I want to froze this part and wait for user's action.
        //When user taps on 'next', it continues working.
        //////////////////////////////////////////////////////
        changeQuestion();
        if (widget.quizType == QuizTypes.audioQuestions) {
          timerAnimationController.value = 0.0;
          showOptionAnimationController.forward();
        } else {
          timerAnimationController.forward(from: 0.0);
        }
        //////////////////////////////////////////////////////
      } else {
        updateSubmittedAnswerForBookmark(context.read<QuestionsCubit>().questions()[currentQuestionIndex]);
        navigateToResultScreen();
      }
    }
  }

When user taps on button, this func continue working

TextButton(
        child: Text("Next >>"),
          onPressed: () {
            setState(() {
              callMyFun();
            });
            //global.setMyStatus == true;
          }
      )
SaturnPro
  • 39
  • 6
  • 1
    Also, I've noticed that you are using `void` instead of `Future`, so this may be useful knowledge for you: https://stackoverflow.com/questions/56244025 – Dmytro Rostopira Jan 11 '22 at 13:08

1 Answers1

2

Disclaimer: while this will solve your problem, it's far away from good practice, you should split your function into two different ones

You can achieve that using Completer

Example:

Completer<void>? nextButtonCompleter;

Future<void> submitAnswer(String submittedAnswer) async {
  // Your code (removed to make answer short)
  // ...
  //I want to froze this part and wait for user's action.
  //When user taps on 'next', it continues working.

  final completer = new Completer<void>();
  nextButtonCompleter = completer;
  // This line will wait until onPressed called
  await completer.future; 
  
  // your other code
}

And in your button

TextButton(
  child: Text("Next >>"),
  onPressed: () {
    setState(() {
      callMyFun();
    });
    nextButtonCompleter?.complete();
    nextButtonCompleter = null;
  }
)
Dmytro Rostopira
  • 10,588
  • 4
  • 64
  • 86