title: How to fix my code to show alert dialog on flutter?
body: <p>I have an app which exports a json object to a json file and while it's exporting, I wanted to show an alert dialog with a circular progress indicator on it. But for some reason, the alert dialog with my progress indicator is not showing up.</p>
  
  <p>This is the look of my app before I export my json: </p>
  
  <p><a href="https://i.stack.imgur.com/M70s7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/M70s7.png" alt="enter image description here"></a></p>
  
  <p>Here is the code for activating the exporting part:</p>
  
  <pre><code>...
  
  child: FlatButton(
    onPressed: () async{
      //Popping the confirm dialog
      Navigator.pop(context);
      //Showing the progress dialog
      showProcessingDialog();
      //Buying some time
      _timer = Timer(Duration(seconds: 5), exportData);
      //Pops the progress dialog
      Navigator.pop(context);
      //Shows the finished dialog
      showFinishedDialog();
      },
    child: Text(
      "Yes",
  </code></pre>
  
  <p>...</p>
  
  <p>After I click 'Yes' in this alert button, it should show the progress dialog but it doesn't show, instead it shows the finished dialog.</p>
  
  <p>Like this:</p>
  
  <p><a href="https://i.stack.imgur.com/XnzY1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XnzY1.png" alt="enter image description here"></a></p>
  
  <p>Here is the code for progress dialog:</p>
  
  <pre><code>void showProcessingDialog() async{
  return showDialog(
  
    barrierDismissible: false,
    context: context,
    builder: (BuildContext context){
  
      return AlertDialog(
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.all(Radius.circular(10.0))),
        contentPadding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
        content: Container(
          width: 250.0,
          height: 100.0,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              CircularProgressIndicator(),
              Text("Exporting...",
                style: TextStyle(
                  fontFamily: "OpenSans",
                  color:  Color(0xFF5B6978)
                  )
                )
            ]
            )
          )
  
  
        );
    }
    );
  }
  </code></pre>
  
  <p>Here is the exportData callback:</p>
  
  <pre><code>void exportData() async{
    List&lt;dynamic&gt; _msgList = await _msgStorage._getList;
    await _expData._saveList(_msgList);
  }
  </code></pre>
  
  <p>I have tried to add Timer class to delay showing finished dialog for 3 seconds but it doesn't work. I can confirm that my json file was exported successfully but the callback of Timer which is the progress dialog didn't show up.</p>
  
  <p>I would appreciate any kind of help.</p>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>I rewrote my code based on the answer of diegoveloper:</p>
  
  <pre><code>onPressed: () async{
  
    Navigator.pop(context);
    print("confirm dialog has pop");
  
    print("showing processdialog");
    showProcessingDialog();
    print("processdialog is being shown.");
  
    print("buying some time");
    await Future.delayed(Duration(seconds: 5));
    print("done buying some time");
  
    print("exporting begin");
    await exportData();
    print("exporting done");
  
    Navigator.pop(context);
    print("processdialog has pop");
  
    print("showing finished dialog");
    showFinishedDialog();
    print("finished dialog is being shown.");
  },
  </code></pre>
  
  <p>At this point, the process dialog is being shown but after printing the "exporting done" and executing the <code>Navigator.pop(context);</code> it gave an error and the process dialog remains in the screen, unpopped. </p>
  
  <p>Like this:</p>
  
  <p><img src="https://i.stack.imgur.com/ABI7H.png" alt="enter image description here"></p>
  
  <pre><code>I/flutter ( 9767): confirm dialog has pop
  I/flutter ( 9767): showing processdialog
  I/flutter ( 9767): processdialog is being shown.
  I/flutter ( 9767): buying some time
  I/flutter ( 9767): done buying some time
  I/flutter ( 9767): exporting begin
  I/flutter ( 9767): exporting done
  E/flutter ( 9767): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
  E/flutter ( 9767): Looking up a deactivated widget's ancestor is unsafe.
  E/flutter ( 9767): 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 inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.
  </code></pre>
  
  <p>After I comment out the <code>await Future.delayed(Duration(seconds: 5));</code> it worked fine.</p>
  
  <p>My question is why did it failed when using <code>Future.delayed</code>?</p>
  
  <p>Here is the full error:</p>
  
  <pre><code>I/flutter ( 9767): confirm dialog has pop
  I/flutter ( 9767): showing processingdialog
  I/flutter ( 9767): processdialog is being shown.
  I/flutter ( 9767): buying some time
  I/flutter ( 9767): done buying some time
  I/flutter ( 9767): exporting begin
  I/flutter ( 9767): exporting done
  E/flutter ( 9767): [ERROR:flutter/shell/common/shell.cc(184)] Dart Error: Unhandled exception:
  E/flutter ( 9767): Looking up a deactivated widget's ancestor is unsafe.
  E/flutter ( 9767): 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 inheritFromWidgetOfExactType() in the widget's didChangeDependencies() method.
  E/flutter ( 9767): 
  E/flutter ( 9767): #0      Element._debugCheckStateIsActiveForAncestorLookup.&lt;anonymous closure&gt; (package:flutter/src/widgets/framework.dart:3246:9)
  E/flutter ( 9767): #1      Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:3255:6)
  E/flutter ( 9767): #2      Element.ancestorStateOfType (package:flutter/src/widgets/framework.dart:3303:12)
  E/flutter ( 9767): #3      Navigator.of (package:flutter/src/widgets/navigator.dart:1288:19)
  E/flutter ( 9767): #4      ChatWindow.showExportedDialog.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:msgdiary/main.dart:368:37)
  E/flutter ( 9767): &lt;asynchronous suspension&gt;
  E/flutter ( 9767): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
  E/flutter ( 9767): #6      _InkResponseState.build.&lt;anonymous closure&gt; (package:flutter/src/material/ink_well.dart:562:30)
  E/flutter ( 9767): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
  E/flutter ( 9767): #8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
  E/flutter ( 9767): #9      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
  E/flutter ( 9767): #10     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
  E/flutter ( 9767): #11     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
  E/flutter ( 9767): #12     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
  E/flutter ( 9767): #13     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:180:19)
  E/flutter ( 9767): #14     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:158:22)
  E/flutter ( 9767): #15     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:138:7)
  E/flutter ( 9767): #16     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
  E/flutter ( 9767): #17     _WidgetsFlutterBinding&amp;BindingBase&amp;GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
  E/flutter ( 9767): #18     _invoke1 (dart:ui/hooks.dart:168:13)
  E/flutter ( 9767): #19     _dispatchPointerDataPacket (dart:ui/hooks.dart:122:5)
  </code></pre>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>It was my fault. I need to study more about context. It seems that I was popping the same context for the two dialogs. I changed the name of the dialog and it worked. </p>