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
the
mounted
bool is an independent variable of theState
, it’s not a member on theBuildContext
, so instead use:BuildContext will soon have a mounted property:
https://github.com/flutter/flutter/pull/111619
context.mounted
This was added in flutter 3.7.0. Running
flutter upgrade
worked for me as I was somehow still on flutter 3.3.x.