0

My Dart compiler is complaining: Don't use 'BuildContext's across async gaps.

  Future<void> check(int forwardCategory, BuildContext context) async {
      ...
      final addDialogResponse = await showDialog<bool>(
        context: context,
        builder: (BuildContext context) => AlertDialog(
          title: const Text('Add relations?'),
          content: Text(
              "Add the following ${widget.relText2} relations:\n$relationsStr"),
          actions: <Widget>[
            TextButton(
              onPressed: () => Navigator.pop(context, false),
              child: const Text('Cancel'),
            ),
            TextButton(
              onPressed: () => Navigator.pop(context, true),
              child: const Text('OK'),
            ),
          ],
        ),
      );
    ...
  }

How to rewrite this code to avoid this warning about displaying a boolean dialog? (And by the way, what's bad in using BuildContext among async gaps?)

porton
  • 5,214
  • 11
  • 47
  • 95
  • Where is it pointing? I think the error is pointing at the code in "..." after the await. After the await, you must be using context again. That context may no longer be mounted so add `if (!context.mounted) return;` and this error will go away. – Randal Schwartz Apr 13 '23 at 06:10
  • @RandalSchwartz Adding `if (!context.mounted) return;` after this statement does not remove the compiler warning. – porton Apr 13 '23 at 08:29

0 Answers0