0

I was getting a warning that said

Don't use 'BuildContext's across async gaps. (Documentation) Try rewriting the code to not reference the 'BuildContext'.

I clicked on the Documentation link and did what they recommended, which was to add the line if (!context.mounted) return; (see below). However, I'm still getting the same warning although now it's pointing to the line I just added. What does this mean and what can I do to code this properly so that the warning doesn't appear? All I need this button to do is to run some asynchronous work and if it was successful then navigate to a different page.

TextButton(
  onPressed: () async {
    final groupID = await database.createGroup();
      if (!context.mounted) return; //Warning points here
      if (groupID == '') {
        Fluttertoast.showToast(msg: 'Error creating group');
      } else {
        groupNameController.clear();
        Navigator.of(context).pop(); //Close the dialog box
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => ScreenChat(groupID: groupID,)),
        );
      }
  },
  child: const Text('OK')
),
whatwhatwhat
  • 1,991
  • 4
  • 31
  • 50
  • Does this answer your question? [Do not use BuildContexts across async gaps](https://stackoverflow.com/questions/68871880/do-not-use-buildcontexts-across-async-gaps) – nvoigt Sep 01 '23 at 05:50

4 Answers4

1

Instead of using:

if (!context.mounted) return;

Try using:

if (!mounted) return;

It will also suppress the warning.

Dharam Budh
  • 515
  • 2
  • 9
0

Instead of using:

if (!context.mounted) return;

Try using:

if (context.mounted) {
} else {
  return;
}

It will suppress the warning.

Dharam Budh
  • 515
  • 2
  • 9
  • That does seem to suppress it, but is that the correct way to go about this? Shouldn't I be refactoring this button so that the warning never appears in the first place? – whatwhatwhat Sep 01 '23 at 04:26
  • I added a new answer. Check it out. – Dharam Budh Sep 01 '23 at 04:31
  • Yes I see the other answer, but my question is if it is good practice to suppress warnings or if I should be changing my code to not make the warning appear – whatwhatwhat Sep 01 '23 at 04:31
  • Yes, it's good practice. See my lint file here: https://gist.github.com/dharambudh1/8e729f5b003f33dac32b1fb40df636bc – Dharam Budh Sep 01 '23 at 04:35
0

The error is saying that the user could close the app or go to another page/screen of your app while the async function is still loading/progressing, which would cause an error after its complete because it can't execute the next line (if/else) or check (!context).

For this reason you need to check if this page/screen is still mounted, and if so you're asking it to continue executing the next lines, or dismiss it. Simply call it this way:

onPressed: () async {
    final groupID = await database.createGroup();
      if (mounted){}
      if (groupID == '') {
        Fluttertoast.showToast(msg: 'Error creating group');
      } else {
        groupNameController.clear();
        Navigator.of(context).pop(); //Close the dialog box
        Navigator.push(
          context,
          MaterialPageRoute(builder: (context) => ScreenChat(groupID: groupID,)),
        );
      }
  },
Texv
  • 1,225
  • 10
  • 14
-1

I dont konow if it is the right way of doing it but declaring,

 final navigator = Navigator.of(context);

at the beginning of the onpressed seems to do the trick,

   onPressed: () async {
       final navigator = Navigator.of(context);//include this line
       final groupID = await database.createGroup();
       //if (mounted){}
    if (groupID == '') {
         Fluttertoast.showToast(msg: 'Error creating group');
    } else {
       groupNameController.clear();
       navigator.of(context).pop(); //updated navigator.
       navigator.push(context, //updated navigator
       MaterialPageRoute(builder: (context) => ScreenChat(groupID: 
       groupID,)),
    );
   }
  },
Anton W
  • 1
  • 1