0

Here I am showing two dialog first is "showAlertDialog" second is "showAlertDialogWithSingleBtn" inside of first one.

In first dialog I am asking to delete account,if user selected yes then i am redirecting on EnterNum() page then after that i am showing second dialog but when I clicking on Ok Btn then its not backing.

This is my dialog functionality.

 showAlertDialog(context, () {
                          Preferences.clearData(context);
                          Navigator.pushAndRemoveUntil(
                            context,
                            MaterialPageRoute(builder: (context) => EnterNum()),
                                (Route<dynamic> route) => false,
                          );
                          showAlertDialogWithSingleBtn(context,(){
                            Navigator.pop(context);
                          },"Alert","Your request for deletion of the account has been accepted and action will be taken in next 30 days.  To cancel your request, kindly login in to your account within 30 days.","Ok");
                        }, "Alert", "Are you sure, you want to delete your account?");

This is my error

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.

When the exception was thrown, this was the stack: 
#0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:4241:9)
#1      Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:4255:6)
#2      Element.findAncestorStateOfType (package:flutter/src/widgets/framework.dart:4322:12)
#3      Navigator.of (package:flutter/src/widgets/navigator.dart:2549:40)
#4      Navigator.pop (package:flutter/src/widgets/navigator.dart:2433:15)
#5      showAlertDialogWithSingleBtn.<anonymous closure> (package:bellaz/Components/alertDialogWithSingleBtn.dart:22:19)
#6      _InkResponseState.handleTap (package:flutter/src/material/ink_well.dart:1072:21)
#7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:253:24)
#8      TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:627:11)
#9      BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:306:5)
#10     BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:239:7)
#11     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:615:9)
#12     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:98:12)
#13     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:143:9)
#14     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:617:13)
#15     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:141:18)
#16     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:127:7)
#17     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:460:19)
#18     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:440:22)
#19     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:337:11)
#20     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:395:7)
#21     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:357:5)
#22     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:314:7)
#23     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:295:7)
#24     _invoke1 (dart:ui/hooks.dart:167:13)
#25     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:341:7)
#26     _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
Handler: "onTap"
Recognizer: TapGestureRecognizer#2141e
  debugOwner: GestureDetector
  state: possible
  won arena
  finalPosition: Offset(205.7, 555.4)
  finalLocalPosition: Offset(42.5, 18.3)
  button: 1
  sent tap down
====================================================================================================

showAlertDialog dialog(First one)

showAlertDialog(BuildContext context,VoidCallback? onPressed,String heading,String msg) {
  // set up the buttons

  Widget cancelButton = TextButton(
    style: TextButton.styleFrom(
      backgroundColor: Color(0xfff64e60),
      padding: EdgeInsetsDirectional.only(start: 36,end: 36),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(2.0),
      ),
    ),
    child: Text(
      "No",
      style: TextStyle(color: Colors.white),
    ),
    onPressed: () {
      Navigator.pop(context);
    },
  );
  Widget continueButton = TextButton(
      style: TextButton.styleFrom(
        backgroundColor:Color(0xff007669),
        padding: EdgeInsetsDirectional.only(start: 34,end: 34),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(2.0),
        ),
      ),
      child: Text(
        "Yes",
        style: TextStyle(color: Colors.white),
      ),
      onPressed:onPressed );

  AlertDialog alert = AlertDialog(
    titlePadding: EdgeInsets.only(top: 13),
    title: Container(
      height: 40,
      child: Center(
        child: Text(
          "$heading",
          style: TextStyle(color:apptitleColor),
        ),
      ),
      // color: Color(0xff007669),
    ),
    content: Text("$msg",textAlign: TextAlign.center,style: TextStyle(fontSize: t3Size),),

    actions: [
      cancelButton,
      continueButton,
    ],
    actionsAlignment: MainAxisAlignment.center,
  );
  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}

showAlertDialogWithSingleBtn dialog(second Dialog)

showAlertDialogWithSingleBtn(BuildContext context,VoidCallback? onPressed,String heading,String msg,String btnText) {
  // set up the buttons


  Widget continueButton = TextButton(
      style: TextButton.styleFrom(
        backgroundColor:Color(0xff007669),
        padding: EdgeInsetsDirectional.only(start: 34,end: 34),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(2.0),
        ),
      ),
      child: Text(
        "$btnText",
        style: TextStyle(color: Colors.white),
      ),
      onPressed:onPressed );

  AlertDialog alert = AlertDialog(
    titlePadding: EdgeInsets.only(top: 13),
    title: Container(
      height: 40,
      child: Center(
        child: Text(
          "$heading",
          style: TextStyle(color:apptitleColor),
        ),
      ),
      // color: Color(0xff007669),
    ),
    content: Text("$msg",textAlign: TextAlign.center,style: TextStyle(fontSize: t3Size),),

    actions: [
      Row(mainAxisAlignment: MainAxisAlignment.center,
        children: [
          continueButton,
        ],
      ),
    ],
    actionsAlignment: MainAxisAlignment.center,
  );
  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return alert;
    },
  );
}
Deepak
  • 1,664
  • 3
  • 19
  • 52

1 Answers1

0

Try to restructure you code with like this.

(take note that you need to refactor your showAlertDialog to resolve with a boolean in order for this to work. Likewise with your deleteAccount method)

The error Looking up a deactivated widgets... is because you are redirecting the user to another screen while the delete method is still in progress..wait for the method to resolve and if successful, then redirect the user.

Future<void> deleteAccount(BuildContext context) async {
final navigator = Navigator.of(context);
  
 final alert = await showAlertDialog(
title: 'Are you sure you want to delete you account?'
);
 
 if(alert == true){
 //execute your delete account method here
final success = await deleteAccount();

if(success){
 //show another alert dialog if deletion is successful
 final confirm = await showAlertDialog(
 title: 'Your request for deletion of the account has been accepted and action will be taken in next 30 days.  To cancel your request, kindly login in to your account within 30 days'
 );
 
 if(confirm == true){
 //handle here if the user pressed the 'Ok' button
  //Redirect the user here to the `EnterNumber()` screen
 }
}

 
 } else {
 navigator.pop();
 }
 
}
john
  • 1,438
  • 8
  • 18