Skip to content

Make it easier to add new final fields to existing constructors #53707

Open
@DanTup

Description

@DanTup

This code:

class A {
  final String a;
  final String b;
}

produces diagnostics for field_not_initialized and has a convenient fix to add a constructor:

image

If I use that fix and later add a new field c:

class A {
  final String a;
  final String b;
  final String c;

  A({required this.a, required this.b});
}

There is no diagnostic on c, nor are there any code actions to add C to the constructor (from the declaration of c, where I was typing). Instead, there is a fix on the constructor:

image

This is less convenient because it requires moving to the constructor and there's no option to add it as a named field (you have to use "Add final field formal parameters" and then "Convert selected formal parameter(s) to named" or "Convert all formal parameters to name" (although that one doesn't move it to the end where you might prefer it)).

It would be useful if a field that is not assigned in a constructor (causing final_not_initialized_constructor) provided a fix/assist at the location of the field (with both named and positional options).

(@bwilkerson one thing I thought about here was having context messages on final_not_initialized_constructor that point at the field, and showing fixes against the locations of context messages in addition to where the diagnostic is rendered... although it may fall down when there are two constructors with this diagnostic pointing at the same field as that would result in two fixes on the field declaration)

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-quick-fixIssues with analysis server (quick) fixesdevexp-refactoringIssues with analysis server refactoringstype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions