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

lint: StatefulWidget could be a StatelessWidget #3683

Open
goderbauer opened this issue Sep 13, 2022 · 4 comments · May be fixed by #3725
Open

lint: StatefulWidget could be a StatelessWidget #3683

goderbauer opened this issue Sep 13, 2022 · 4 comments · May be fixed by #3725
Labels
lint-request status-accepted type-enhancement A request for a change that isn't a bug

Comments

@goderbauer
Copy link
Contributor

Occasionally, I see Flutter code where people use StatefulWidgets even though a StatelessWidget would have been sufficient. Example:

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

The State class doesn't store any state in member variables and it also doesn't use any of the properties provided by Sate (e.g. mounted). This could easily be refactored to be a more concise StatelessWidget:

class MyWidget extends StatelessWidget {
  const MyWidget({super.key});

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

I wonder if it would be possible to have a lint for this?

Of course, from a functional perspective using a StatefulWidget here is totally fine, the additional performance overhead of a StatefulWidget over a StatelessWidget is probably neglectable. However, using a StatelessWidget where appropriate leads to more compact, concise, and readable code and is more idiomatic, IMO.

@bwilkerson
Copy link
Member

I wonder if it would be possible to have a lint for this?

Yes, the lint appears to be well defined, so it should be possible to implement it. I'm not sure how often code like this is written, so it's hard to gauge how much value such a lint would bring to users, but it seems doable.

@elbeicktalat
Copy link

I'm not sure how often code like this is written
@bwilkerson I agree with about the complexity, but if we make the following simple checks than I think we are good to go.

  1. Check if widget hasn't a setState
  2. Check if the widget hasn't a life cycle methods
  3. Check if the widget hasn't any non final field (optional)

@a14n a14n linked a pull request Sep 23, 2022 that will close this issue
@srawlins
Copy link
Member

@pq as this rule is Flutter-specific, I'm not sure how many "yays" you want in order to declare it "accepted." FWIW there isn't any disagreement.

@pq
Copy link
Member

pq commented Apr 17, 2023

Looks good to me. Thanks!

@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lint-request status-accepted type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants