0

How to rewrite this to awoid Don't use 'BuildContext's across async gaps. warning.

onPressed: () async {
  if (snapshot.hasData) {
    final isRapportExist = await bloc.isRapportExist();

    if (isRapportExist) {
      return;
    }

    bloc.submitRapport();
    Navigator.pop(context, true); //Don't use 'BuildContext's across async gaps.
  }
},

In such cases usully mounted property used, but where it comes from?

if(!mounted) return; //Undefined name 'mounted'.
Navigator.pop(context, true);
rozerro
  • 5,787
  • 9
  • 46
  • 94
  • Does this answer your question? [how to solve BuildContexts across async gaps error flutter](https://stackoverflow.com/questions/73527600/how-to-solve-buildcontexts-across-async-gaps-error-flutter) – Robert Sandberg Apr 18 '23 at 09:57
  • 1
    mounted is in State, or if you're in newer flutter, also in BuildContext. – Randal Schwartz Apr 18 '23 at 18:02

2 Answers2

1

You can access it from context (BuildContext).

if(!context.mounted) return;
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
0

It's a property of the State class - which your State extends.

https://api.flutter.dev/flutter/widgets/State-class.html

Andrija
  • 1,534
  • 3
  • 10