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

Option to "Convert to Stateless Widget" #46011

Closed
ghost opened this issue May 13, 2021 · 9 comments
Closed

Option to "Convert to Stateless Widget" #46011

ghost opened this issue May 13, 2021 · 9 comments
Labels
analyzer-refactoring area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@ghost
Copy link

ghost commented May 13, 2021

@ThinkDigitalSoftware commented on Aug 5, 2019, 5:14 PM UTC:

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

This issue was moved by helin24 from flutter/flutter-intellij#3734.

@ghost
Copy link
Author

ghost commented May 13, 2021

@stevemessick commented on Aug 6, 2019, 2:49 PM UTC:

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

@ghost
Copy link
Author

ghost commented May 13, 2021

@ThinkDigitalSoftware commented on Aug 6, 2019, 6:04 PM UTC:

I figured it would be natural to add the companion. Also, it wouldn't be present at the same time as the convert to stateful, so the list length would stay the same

On Tue, Aug 6, 2019, 7:49 AM stevemessick ***@***.***> wrote: @scheglov <[https://github.com/scheglov](https://github.com/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. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub <[#3734](https://github.com/flutter/flutter-intellij/issues/3734)?email_source=notifications&email_token=AFPYO7OK2PAP2YBPDGU6AMTQDGFQLA5CNFSM4IJNHC6KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3VMRAQ#issuecomment-518703234>, or mute the thread <[https://github.com/notifications/unsubscribe-auth/AFPYO7I2LKBUKAWLVXFS4BTQDGFQLANCNFSM4IJNHC6A](https://github.com/notifications/unsubscribe-auth/AFPYO7I2LKBUKAWLVXFS4BTQDGFQLANCNFSM4IJNHC6A)> .

@ghost
Copy link
Author

ghost commented May 13, 2021

@scheglov commented on Aug 6, 2019, 6:05 PM UTC:

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

@ghost
Copy link
Author

ghost commented May 13, 2021

@ThinkDigitalSoftware commented on Aug 6, 2019, 6:14 PM UTC:

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.

@ghost
Copy link
Author

ghost commented May 13, 2021

@jacob314 commented on Apr 21, 2020, 3:26 PM UTC:

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.

@srawlins srawlins added analyzer-refactoring area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug labels May 14, 2021
@asashour
Copy link
Contributor

asashour commented Sep 2, 2021

Should this be a lint and moved to linter project? or should it be an assist or refactoring in sdk project?

@bwilkerson
Copy link
Member

It would be an assist, similar to FlutterConvertToStatefulWidget.

@jacob314
Copy link
Member

jacob314 commented May 6, 2022

We should also add a lint as a lint + quickfix makes it 10X more discoverable than just an assist.
The lint would warn any time you have a stateful widget with only (non-late) final fields in the state.

@asashour
Copy link
Contributor

asashour commented Jun 29, 2022

sdk/250087, which currently proposes:

Widget can not be Stateless if there is any of:

  • a call to one of State methods (outside a default override of calling the super method)
  • a static method or field in State subclass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-refactoring area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants