skip to Main Content

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);

2

Answers


  1. It’s a property of the State class – which your State extends.

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

    Login or Signup to reply.
  2. You can access it from context (BuildContext).

    if(!context.mounted) return;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search