You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be beneficial to have a linter feature that catches errors related to the loss of type information in generic class definitions, especially in the context of Flutter's Stateful Widgets. This issue often arises when the state class does not properly specify the generic type on the State class, leading to potential type safety issues at run time.
For example, the following code would cause a runtime error when using the onTap method. Something along the lines of type '(MyConcreteTypeImplementation) => void' is not a subtype of type '(dynamic) => void'.
classMyGenericWidget<T> extendsStatefulWidget {
finalT item;
finalvoidFunction(T) onTap;
// Imagine a constructor here@override_MyGenericWidgetStatecreateState() =>_MyGenericWidgetState<T>();
}
class_MyGenericWidgetState<T> extendsState<MyGenericWidget> {
// Imagine there is a widget here that calls the onTap with a generic type T.
}
This is because when declaring the _MyGenericWidgetState the generic T is not passed to MyGenericWidget.
In the given example, it would have to be declared as:
In my opinion this is extremely easy to overlook and can cause a lot of confusion, especially to newer flutter developers who have not ran into this exact issue before.
I think a linter rule would be a good solution. Even something as case-specific as the MyStatefulWidget class accepts a generic, therefore when extending State and passing the MyStatefulWidget (State<MyStatefulWidget>), the generic values for MyStatefulWidget must also be passed (State<MyStatefulWidget<T>>).
The text was updated successfully, but these errors were encountered:
@Tomburgs
This would belong to the repo mentioned above. flutter_lints is a plugin to enable existing lints but since you are looking to actually add a new linter rule, it need to be implemented in dart first.
Closing from here.
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.
Use case
It would be beneficial to have a linter feature that catches errors related to the loss of type information in generic class definitions, especially in the context of Flutter's Stateful Widgets. This issue often arises when the state class does not properly specify the generic type on the
State
class, leading to potential type safety issues at run time.For example, the following code would cause a runtime error when using the
onTap
method. Something along the lines oftype '(MyConcreteTypeImplementation) => void' is not a subtype of type '(dynamic) => void'
.This is because when declaring the
_MyGenericWidgetState
the genericT
is not passed toMyGenericWidget
.In the given example, it would have to be declared as:
In my opinion this is extremely easy to overlook and can cause a lot of confusion, especially to newer flutter developers who have not ran into this exact issue before.
Related issues:
Proposal
I think a linter rule would be a good solution. Even something as case-specific as the
MyStatefulWidget
class accepts a generic, therefore when extendingState
and passing theMyStatefulWidget
(State<MyStatefulWidget>
), the generic values forMyStatefulWidget
must also be passed (State<MyStatefulWidget<T>>
).The text was updated successfully, but these errors were encountered: