Reputation: 1084
I have a AlertDialog on the click of Ok button of alertdialog I am getting the data from server. Till the data is received I am displaying another loading dialog. But the loading dialog is not closing once the data is received.
Here is my code:
final GlobalKey<State> _keyLoader = new GlobalKey<State>();
Future<void> _handleSubmit(BuildContext context,id) async {
showDialog(
context: context,
builder: (BuildContext dialogcontext) {
return AlertDialog(
title: Text(
'Topup',
),
content: Text('Are you sure?'),
actions: <Widget>[
new FlatButton(
child: new Text('Ok'),
onPressed: () async {
Navigator.of(dialogcontext).pop();
_submit(dialogcontext, id);
},
),
new FlatButton(
child: new Text('Cancel'),
onPressed: () {
Navigator.of(dialogcontext).pop();
},
)
],
);
});
}
Future<void> _submit(BuildContext context, int id) async {
try {
Dialogs.showLoadingDialog(
context, _keyLoader, "Please Wait..."); //invoking login
await getData(id, context);
Navigator.of(_keyLoader.currentContext, rootNavigator: true)
.pop(); //close the dialoge
} catch (error) {
print(error);
}
}
And this is my Dialogs class for showing loading dialog
class Dialogs {
static Future<void> showLoadingDialog(
BuildContext context, GlobalKey key, String textToShow) async {
return showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return new WillPopScope(
onWillPop: () async => false,
child: SimpleDialog(key: key,
// backgroundColor: Colors.black54,
children: <Widget>[
Center(
child: Column(children: [
CircularProgressIndicator(
//backgroundColor: Colors.purple,
valueColor: new AlwaysStoppedAnimation<Color>(
Colors.pinkAccent[400]),
),
SizedBox(
height: 10,
),
Text(
textToShow,
style: TextStyle(color: Colors.pinkAccent[400]),
)
]),
)
]));
});
}
}
Also in Logs I am getting:
Looking up a deactivated widget's ancestor is unsafe.
I/flutter (21400): At this point the state of the widget's element tree is no longer stable.
I/flutter (21400): 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.
Upvotes: 0
Views: 263
Reputation: 15053
Try this, Read TODO
comments
Future<void> _handleSubmit(BuildContext context, id) async {
showDialog(
context: context,
builder: (BuildContext dialogContext) {
return AlertDialog(
title: Text('Topup'),
content: Text('Are you sure?'),
actions: <Widget>[
new FlatButton(
child: new Text('OK'),
onPressed: () async {
Navigator.of(dialogContext).pop();
_submit(context, id); //TODO: Don't send dialog context since we already did pop on it
},
),
new FlatButton(
child: new Text('Cancel'),
onPressed: () {
Navigator.of(dialogContext).pop();
},
)
],
);
},
);
}
Future<void> _submit(BuildContext context, int id) async {
try {
Dialogs.showLoadingDialog(
context, _keyLoader, "Please Wait..."); //invoking login
await getData(id, context);
await Future.delayed(Duration.zero); //TODO: Add this line
Navigator.of(context).pop(); //TODO: Use the received context
} catch (error) {
print(error);
}
}
Upvotes: 1