Skip to content

Option to "Convert to Stateless Widget" #3734

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

Closed
ThinkDigitalSoftware opened this issue Aug 5, 2019 · 6 comments
Closed

Option to "Convert to Stateless Widget" #3734

ThinkDigitalSoftware opened this issue Aug 5, 2019 · 6 comments

Comments

@ThinkDigitalSoftware
Copy link

Can we have the option to convert to a stateless widget if all variables if any are immutable?

@stevemessick
Copy link
Member

@scheglov Does this seem like a reasonable request? I'd be concerned that our list of actions might get too large if we do everything.

@stevemessick stevemessick added this to the On Deck milestone Aug 6, 2019
@ThinkDigitalSoftware
Copy link
Author

ThinkDigitalSoftware commented Aug 6, 2019 via email

@scheglov
Copy link
Contributor

scheglov commented Aug 6, 2019

@ThinkDigitalSoftware could you please provide a couple of example when it should happen, and when not?

@ThinkDigitalSoftware
Copy link
Author

Sure. In this case, it should show up, since this is an easy conversion with no possible side effects. I.E. move the build function and delete the createState.

class SampleWidget extends StatefulWidget {
  @override
  _SampleWidgetState createState() => _SampleWidgetState();
}

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

In this case, all variables are immutable. No setState calls.

class SampleWidget extends StatefulWidget {
  @override
  _SampleWidgetState createState() => _SampleWidgetState();
}

class _SampleWidgetState extends State<SampleWidget> {
  final variableA = 7;
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

no variables are reassigned here, no setState calls. variable can be made immutable and moved over.

class SampleWidget2 extends StatefulWidget {
  @override
  _SampleWidget2State createState() => _SampleWidget2State();
}

class _SampleWidget2State extends State<SampleWidget2> {
  String aString = "Hello, world";
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

The cases where it shouldn't show up are all the cases not shown here. If a widget has state that's internally managed, it's not available for that refactor. If setState is used...

My use case is that I switched to managing the state of my widget to outside of the widget. I moved out all the mutable variables and then had to cut, paste and delete (and change Stateful to Stateless) to get it done.

@jacob314
Copy link
Contributor

I've found myself wanting this refactor a few times. Often you have a stateful widget and then you perform some refactors so it can be stateless but it takes a lot of manual boilerplate renames to make it a stateless widget (e.g. remove widget. in lots of places so it would be very handy to have this lint and quickfix.

@ghost ghost deleted a comment from helin24 May 13, 2021
@ghost
Copy link

ghost commented May 13, 2021

This issue was moved by helin24 to dart-lang/sdk#46011.

@ghost ghost closed this as completed May 13, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants