'Exception caught by gesture in Alert DialogButton

I have a Quiz app that after user choose wrong/good answer jumping alert on screen. the alert button work well and close the alert if the user click the button before the animation move to the next question(3 sec timer), but when the animation move the next question before the user click on the button, the alert button not responding and the user cant close the alert.

I get this error:

======== Exception caught by gesture =============================================================== The following assertion was thrown while handling a gesture: Looking up a deactivated widget's ancestor is unsafe.

At this point the state of the widget's element tree is no longer stable.

To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

this is my alert code:

  void checkAnswer() {
    if (qnController.isAnswered) {
      if (index == qnController.selectedAns &&
          qnController.selectedAns == qnController.correctAns) {
        var alertStyle = AlertStyle(
          isCloseButton: false,
          isOverlayTapDismiss: true,
          //TODO need to set to false after fix the bug
          animationDuration: Duration(milliseconds: 5),
        );
        Alert(
          context: mainContext,
          //type: AlertType.error,
          style: alertStyle,
          image: Image.asset("assets/icons/smile.png"),
          title: "good",
          buttons: [
            DialogButton(
              child: Text(
                "next",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
              onPressed: () => Navigator.pop(mainContext, false),
              color: Color.fromRGBO(0, 179, 134, 1.0),
              radius: BorderRadius.circular(0.0),
            ),
          ],
        ).show(); // Show Pop UP Good answer
      } else {
        if (index == qnController.selectedAns &&
            qnController.selectedAns != qnController.correctAns) {
          var alertStyle = AlertStyle(
              isCloseButton: false,
              isOverlayTapDismiss: false,
              animationDuration: Duration(milliseconds: 400));
          Alert(
            context: mainContext,
            //type: AlertType.error,
            style: alertStyle,
            image: Image.asset("assets/icons/sad.png"),
            title: "wrong",
            buttons: [
              DialogButton(
                child: Text(
                  "try again",
                  style: TextStyle(color: Colors.white, fontSize: 20),
                ),
                onPressed: () => Navigator.pop(mainContext),
                color: Color.fromRGBO(0, 179, 134, 1.0),
                radius: BorderRadius.circular(0.0),
              ),
            ],
          ).show(); // Show Pop UP wrong answer

        }
      }
      return;
    }
  }

Would appreciate if someone could explain why I'm getting this error and post a rectified version of this code?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source