skip to Main Content

I am trying to access to ‘mounted’ property, as specified in the Flutter docs.

With the following code…

      if (context.mounted) {

      }

I get the following error…

The getter ‘mounted’ isn’t defined for the type ‘BuildContext’.
Try importing the library that defines ‘mounted’, correcting the name to the name of an existing getter, or defining a getter or field named ‘mounted’.

I am on the latest flutter version avaible by running ‘upgrade’…

Flutter 3.3.10 • channel stable •
https://github.com/flutter/flutter.git Framework • revision 135454af32
(2 weeks ago) • 2022-12-15 07:36:55 -0800 Engine • revision 3316dd8728
Tools • Dart 2.18.6 • DevTools 2.15.0

3

Answers


  1. the mounted bool is an independent variable of the State, it’s not a member on the BuildContext, so instead use:

      if (mounted) {
    
      }
    
    Login or Signup to reply.
  2. BuildContext will soon have a mounted property:
    https://github.com/flutter/flutter/pull/111619

    context.mounted

    Login or Signup to reply.
  3. This was added in flutter 3.7.0. Running flutter upgrade worked for me as I was somehow still on flutter 3.3.x.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search