Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linter rule to catch type information loss in generic state class definitions #147369

Closed
Tomburgs opened this issue Apr 25, 2024 · 3 comments
Closed
Labels
r: invalid Issue is closed as not valid

Comments

@Tomburgs
Copy link

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 of type '(MyConcreteTypeImplementation) => void' is not a subtype of type '(dynamic) => void'.

class MyGenericWidget<T> extends StatefulWidget {
  final T item;
  final void Function(T) onTap;

  // Imagine a constructor here

  @override
  _MyGenericWidgetState createState() => _MyGenericWidgetState<T>();
}

class _MyGenericWidgetState<T> extends State<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:

class _MyGenericWidgetState<T> extends State<MyGenericWidget<T>>

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 extending State and passing the MyStatefulWidget (State<MyStatefulWidget>), the generic values for MyStatefulWidget must also be passed (State<MyStatefulWidget<T>>).

@bartekpacia
Copy link
Member

I think this issue belongs to https://github.com/dart-lang/linter

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Apr 26, 2024
@darshankawar
Copy link
Member

@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.

@darshankawar darshankawar closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2024
@darshankawar darshankawar added r: invalid Issue is closed as not valid and removed in triage Presently being triaged by the triage team labels Apr 26, 2024
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
r: invalid Issue is closed as not valid
Projects
None yet
Development

No branches or pull requests

3 participants