Skip to content

Add key to constructor fix generates new diagnostic #63965

Description

@FMorschel

Consider (enable use_key_in_widget_constructors lint):

import 'package:flutter/widgets.dart';

class Foo extends StatelessWidget {
  final String txt = getNonConstString();

  static String getNonConstString() => 'Hello World';

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

It would generate:

class Foo extends StatelessWidget {
  final String txt = getNonConstString();
//                   ^^^^^^^^^^^^^^^^^ const_eval_method_invocation

  const Foo({super.key});
//^^^^^ const_constructor_with_field_initialized_by_non_const

  static String getNonConstString() => 'Hello World';

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

We should be able to detect that the const version would be invalid and omit the const then.

CC @bwilkerson @srawlins

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-quick-fixIssues with analysis server (quick) fixestype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions