Closed Tomburgs closed 5 months ago
previously opened in flutter: https://github.com/flutter/flutter/issues/147369
I think what you're looking for is static analysis that reports 'raw types'. You can enable 'strict-raw-types' as seen here: https://dart.dev/tools/analysis#enabling-additional-type-checks
loss_of_type_information
Alternatively
loss_of_type_information_in_state
would be more explicit.Description
State implementation did not pass generics back to StatefulWidget declaration
Details
It would be beneficial to have a linter rule 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:Kind
This would be a flutter rule that would guard against runtime errors. Example of an error an incorrect implementation could cause:
type '(MyConcreteTypeImplementation) => void' is not a subtype of type '(dynamic) => void'
Bad Examples
This should fire in the following example, because
MyGenericWidget
and_MyGenericWidgetState
both accept the same generic (implies that it is passed on), however in theextends State<MyGenericWidget>
the genericT
is not passed on.Good Examples
The correct implementation of this rule would expect the generic to be passed on to the
State<MyGenericWidget>
.Discussion
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. To back this point, it is not difficult to find issues / StackOverflow questions asking about this exact issue.
Some of the aforementioned issues:
I don't believe such rule would overlap with any other rules. I've given the existing rules a look and could not find anything similar. Granted I'm not all that knowledgable in the dart linter workings, so feel free to comment / correct this if you, the reader, think it does overlap with something.