Do all widgets in Flutter have the "bool this.mounted" property?
Asked Answered
I

2

5

Peace be upon you

I am researching Flutter's widget lifecycle and stopped at a point which is the this.mounted boolean variable, it does exist directly in the State class, which can only be connected with a StatefulWidget as a subclass and never will/did with a StatelessWidget.

The question is ...

Do all widgets actually contain the bool this.mounted variable as some say here in [this accepted answer, this article and this ], or not?

I searched in Flutter and found this mounted variable only in the State class, then I looked further to find a function called mount() inside the Element class.

The StatelessWidget class is connected to the StatelessElement class, which extends from the ComponentElement that inherits directly from the Element class.

I'm really confused right now, maybe I misunderstood this, I need an explanation for this please!, Thank you.

Iq answered 19/5, 2021 at 10:28 Comment(1)
There is an option to get mounted getter using wrapped stateful widget https://mcmap.net/q/355996/-check-if-stateless-widget-is-disposed-in-flutterBore
M
3

Not sure why that answers says all widgets. But NO.

Only State<T> objects are the ones that have a getter named mounted.

This is easy to verify, Just cmd + click or ctrl + click on any State you have in your class which will redirect you to a file called framework.dart which is located at flutter -> package -> flutter -> lib -> src -> widgets path.

Search mounted and this line is the only actual declaration that you will find.

bool get mounted => _element != null;

And that's part of the State class' code.

Marzi answered 19/5, 2021 at 14:11 Comment(5)
Yes, I agree with you my friend but I was thinking maybe .. just maybe, StatelessWidget has a "mounted" boolean but it's private because we don't need a getter for it since the widget is immutable ?!, Just wondering.Iq
Even if it were private, it would still show up in the code, right ? I have searched through the code for mentions of mounted and only the State widget had that variable. You can open the framework code at the path I have mentioned and check yourselves.Marzi
Private or public, the variable should atleast be in the code. If there were something with the same idea but with some different name, then we wouldn't know. But for sure, there is no other mounted in any other class.Marzi
Alright then, I'll accept your answer cuz we can't deny the fact that there's no mounted property other than the one in the State class.Iq
Cool Thanks. I'll do some more research on the actual _element variable that the mounted is based on and update you if I get across anything.Marzi
L
14

In the future versions of Flutter (currently 3.3.4 on the stable channel) BuildContext will have a getter bool mounted so you can use it in stateless widgets:

See this PR which adds the getter: https://github.com/flutter/flutter/pull/111619

Update:

This has been released with Flutter 3.7.0. You can now use context.mounted in stateful and stateless widgets.

Lanoralanose answered 12/10, 2022 at 3:3 Comment(2)
Valentin updated that this will be available >= 3.4.0, possibly later.Zulema
context.mounted is soo pretty to StatelessWidget`s. thanksMyrtie
M
3

Not sure why that answers says all widgets. But NO.

Only State<T> objects are the ones that have a getter named mounted.

This is easy to verify, Just cmd + click or ctrl + click on any State you have in your class which will redirect you to a file called framework.dart which is located at flutter -> package -> flutter -> lib -> src -> widgets path.

Search mounted and this line is the only actual declaration that you will find.

bool get mounted => _element != null;

And that's part of the State class' code.

Marzi answered 19/5, 2021 at 14:11 Comment(5)
Yes, I agree with you my friend but I was thinking maybe .. just maybe, StatelessWidget has a "mounted" boolean but it's private because we don't need a getter for it since the widget is immutable ?!, Just wondering.Iq
Even if it were private, it would still show up in the code, right ? I have searched through the code for mentions of mounted and only the State widget had that variable. You can open the framework code at the path I have mentioned and check yourselves.Marzi
Private or public, the variable should atleast be in the code. If there were something with the same idea but with some different name, then we wouldn't know. But for sure, there is no other mounted in any other class.Marzi
Alright then, I'll accept your answer cuz we can't deny the fact that there's no mounted property other than the one in the State class.Iq
Cool Thanks. I'll do some more research on the actual _element variable that the mounted is based on and update you if I get across anything.Marzi

© 2022 - 2024 — McMap. All rights reserved.