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");
    }
  }
}
1

1 Answer 1

1

Try like this 😅:

Future checkBoxSealMatching(BuildContext context) async {
final focusScope = FocusScope.of(context);
FocusNode boxFocusNode = FocusNode();
...
        if (boxFieldController.text == barcode) {
          focusScope.requestFocus(locationFocusNode);
        }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for your answer and this is also the right answer for many other situations
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 } }
Get it right at the beginning something like this final teamFuture = getTeam(context);. When necessary, just wait for the future final team = await teamFuture;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.