I've tried to build this:
child: GestureDetector(
onTap: () {
return showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
content: Text('Some text'),
actions: <Widget>[
TextButton(
child: Text('Yes'),
onPressed: () async {
await myFunc(context);
},
),
Here is code of myFunc:
Future myFunc(BuildContext context) async {
await Future.delayed(Duration(seconds: 2));
Navigator.of(context).pop();
showErrorDialog(context); //return new showDialog
}
Right now it works fine, but I want to pop the first showDialog before the delay, and I try this way:
Future myFunc(BuildContext context) async {
Navigator.of(context).pop();
await Future.delayed(Duration(seconds: 2));
showErrorDialog(context); //return new showDialog
}
And now showErrorDialog is not called, and I get the following:
E/flutter ( 9151): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter ( 9151): At this point the state of the widget's element tree is no longer stable.
E/flutter ( 9151): 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.
E/flutter ( 9151): #0 Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:3864:9)
E/flutter ( 9151): #1 Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:3878:6)
E/flutter ( 9151): #2 Element.dependOnInheritedWidgetOfExactType (package:flutter/src/widgets/framework.dart:3893:12)
E/flutter ( 9151): #3 Localizations.of (package:flutter/src/widgets/localizations.dart:472:48)
E/flutter ( 9151): #4 debugCheckHasMaterialLocalizations.<anonymous closure> (package:flutter/src/material/debug.dart:70:23)
E/flutter ( 9151): #5 debugCheckHasMaterialLocalizations (package:flutter/src/material/debug.dart:91:4)
E/flutter ( 9151): #6 showDialog (package:flutter/src/material/dialog.dart:1049:10)
...
How do I call pop before the delay and not get an error?
Change Your functions like this :
myFunc :
Future myFunc(BuildContext ctx , BuildContext context) async {
Navigator.pop(ctx);
await Future.delayed(Duration(seconds: 2));
showErrorDialog(context);
}
GestureDetector :
child: GestureDetector(
onTap: () {
return showDialog(
context: context,
builder: (BuildContext ctx) {
return AlertDialog(
content: Text('Some text'),
actions: <Widget>[
TextButton(
child: Text('Yes'),
onPressed: () async {
await myFunc(ctx ,context);
},
),