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

[analyzer] ConvertToStateless should consider non-constant fields #50682

Open
asashour opened this issue Dec 11, 2022 · 2 comments
Open

[analyzer] ConvertToStateless should consider non-constant fields #50682

asashour opened this issue Dec 11, 2022 · 2 comments
Labels
analyzer-assist Issues with analysis server assists 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-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@asashour
Copy link
Contributor

asashour commented Dec 11, 2022

import 'package:flutter/material.dart';

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

  @override
  State<Abc> createState() => _AbcState();
}

class _AbcState extends State<Abc> {
  GlobalKey<FormState> key = GlobalKey();

  @override
  Widget build(BuildContext context) => Container();
}

has an assist to convert it to

import 'package:flutter/material.dart';

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

  GlobalKey<FormState> key = GlobalKey();

  @override
  Widget build(BuildContext context) => Container();
}

which gives a compile error, because const constructor conflicts with the non-constant field.

In this case, it is better to also remove the const keyword. This would also lead to the necessity of the removal of const in the construction invocation, but that's outside the scope of the assist.

@lrhn lrhn added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Dec 12, 2022
@bwilkerson bwilkerson added P3 A lower priority bug or feature request analyzer-assist Issues with analysis server assists labels Dec 13, 2022
@bwilkerson
Copy link
Member

I don't know how often this is impacting users, so the priority might need to be adjusted.

@bwilkerson
Copy link
Member

In this case, it is better to also remove the const keyword.

It isn't clear to me whether we want to remove the const keyword or whether we want to not offer the assist in this case. The reason I think it might not be good to remove the const keyword is that doing so could introduce errors anywhere that the class is being instantiated in a constant context.

It's possible that when the new refactoring support is completed we might want to consider making this a refactoring (which would allow us to clean up visible use cases). (I'm not sure that's ideal either, but it's worth considering.)

@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-assist Issues with analysis server assists 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-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants