Search code examples
flutterdart

Don't use 'BuildContext's across async gaps, guarded by an unrelated 'mounted' check


Why is the error saying "Don't use 'BuildContext's across async gaps, guarded by an unrelated 'mounted' check."? Is the mounted getter deprecated?

if (context.mounted) {
  pop(context);
}

Solution

  • Now in newer version of flutter you can use mounted directly inside StatefulWidget

    instead of using

    if (context.mounted) {
      pop(context);
    }
    

    Use

    if (mounted) {
      pop(context);
    }
    

    you can check the details when to use context.mounted and mounted here: use_build_context_synchronously