0

In the provider model I use context after await It shows warning Do not use BuildContexts across async gaps and I am unable to check with mounted because I use it in Provider class. so what I should do to remove this warning? Any help would be appreciated thanks.

class SoloStartSiteScanningProvider extends ChangeNotifier {
  FocusNode boxFocusNode = FocusNode();
  FocusNode locationFocusNode = FocusNode();
  Future checkBoxSealMatching(BuildContext context) async {
    try {    
      var barcode = await _scanBoxBarcode();
      if (barcode != "-1") {
        if (boxFieldController.text == barcode) {
          // Do not use BuildContexts across async gaps
          FocusScope.of(context).requestFocus(locationFocusNode);
        }
      }
    } catch (e) {
      print(" coll lactaion scanning Exception=== $e");
    }
  }
}
its-me-mahmud
  • 690
  • 1
  • 7
  • 14
Abdullah
  • 35
  • 1
  • 9
  • Does this answer your question? [Do not use BuildContexts across async gaps](https://stackoverflow.com/questions/68871880/do-not-use-buildcontexts-across-async-gaps) – Georgina Dec 09 '22 at 17:35

1 Answers1

1

Try like this :

Future checkBoxSealMatching(BuildContext context) async {
final focusScope = FocusScope.of(context);
FocusNode boxFocusNode = FocusNode();
...
        if (boxFieldController.text == barcode) {
          focusScope.requestFocus(locationFocusNode);
        }
}
Ruble
  • 2,589
  • 3
  • 6
  • 29
  • 1
    Thanks a lot for your answer and this is also the right answer for many other situations – Abdullah Dec 10 '22 at 08:56
  • what we can do In this scenario if you have any idea plz : Future checkBoxSealMatching(BuildContext context) async { final focusScope = FocusScope.of(context); if (boxFieldController.text == barcode) { getTeam(context); // Do not use BuildContexts across async gaps } } – Abdullah Dec 10 '22 at 12:45
  • Get it right at the beginning something like this `final teamFuture = getTeam(context);`. When necessary, just wait for the future `final team = await teamFuture;` – Ruble Dec 10 '22 at 13:17