I have slightly strange issue (I am probably missing something)
I decided to iterate over element tree in Flutter and had the following question:
if (element.widget.runtimeType == GestureDetector) {
print('return true1');
}
if (element.widget.runtimeType is GestureDetector) {
print('return true2');
}
The runtype of my widget seems to be GestureDetector
(that what I see in the debugger).
When I compare with ==
I see the return true1 as expected.
BUT why when I compare it with is
operator I don’t see the return true2
That’s kinda strange,
why the is
operator doesn’t return true2?
according to docs:
The result of obj is T is true if obj implements the interface specified by T
link
2
Answers
You should test the object itself, not its
runtimeType
getter. So it should be like this:See the usage of Dart type test operators: https://dart.dev/language/operators#type-test-operators
runtimeType is a property available to every object in dart. It has type of ‘Type’.
So when you do
it is checking if runtimeType is GestureDetector where the value of
runtimeType
is GestureDetector but type isType
.When you are doing,
It is checking of type of runtimeType is GestureDetector, which is not true as it is Type.
To check if widget is GestureDetector, you should be doing