I got this simple build function which should show a dialog when the snapshot
has an error, but I'm unable to do so without getting the following error:
flutter: The following assertion was thrown building FutureBuilder(dirty, dependencies: flutter: [_LocalizationsScope-[GlobalKey#f4f3f], _InheritedTheme], state: flutter: _FutureBuilderState#175f8): flutter: setState() or markNeedsBuild() called during build. flutter: This Overlay widget cannot be marked as needing to build because the framework is already in the flutter: process of building widgets. A widget can be marked as needing to be built during the build phase flutter: only if one of its ancestors is currently building. This exception is allowed because the framework flutter: builds parent widgets before children, which means a dirty descendant will always be built. flutter: Otherwise, the framework might not visit this widget during this build phase. flutter: The widget on which setState() or markNeedsBuild() was called was: flutter:
Overlay-[LabeledGlobalKey#07fd4]
Code:
showUnknownErrorAlertDialog(BuildContext context, [void Function() onClose]) {
showDialog(
context: context,
builder: (BuildContext builderContext) {
return AlertDialog(
title: Text(AppLocalizations.of(builderContext).lang['unknownError']),
actions: <Widget>[
FlatButton(
child: Text(AppLocalizations.of(builderContext).lang['close']),
onPressed: () {
Navigator.of(builderContext).pop();
if (onClose != null) {
onClose();
}
},
),
],
);
}
);
}
goBack() {
locator<AppNavigation>().navigateTo('gameOverview', AppGameArgs(gameName));
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: getGameSession(),
builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return showUnknownErrorAlertDialog(context, () {
goBack();
});
}
/ ... omitted code .../
How am I supposed to handle errors in the build function if I'm not allowed to show a dialog or navigate away instantly when there's an error?