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
>Solution :
runtimeType is a property available to every object in dart. It has type of ‘Type’.
So when you do
if (element.widget.runtimeType == GestureDetector) {
print('return true1');
}
it is checking if runtimeType is GestureDetector where the value of runtimeType is GestureDetector but type is Type.
When you are doing,
if (element.widget.runtimeType is GestureDetector) {
print('return true2');
}
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
if (element.widget is GestureDetector) {
print('return true2');
}