Skip to content

Use primary constructors in the flame create templates #4224

Closed

Description

@spydon

What could be improved

The basics and example templates of flame create declare their constructors with the classic
syntax, for example Player() : super(...). flame_lint enables use_primary_constructors since
1.5.0-dev.1, which reports every one of them:

  • basics: the Player constructor in lib/main.dart.
  • example: the MyGame constructor in lib/my_game.dart, the Player constructor in
    lib/player.dart and the Star constructor in lib/star.dart.

They are defined in packages/flame_cli/lib/src/create_templates.dart, and should be rewritten as
primary constructors:

class Player() extends RectangleComponent with TapCallbacks {
  this
    : super(
        size: Vector2.all(64),
        anchor: Anchor.center,
        paint: BasicPalette.white.paint(),
      );
}

Analyzing the same constructor patterns with the flame_lint from main gives:

info • Use a primary constructor. Try using a primary constructor • use_primary_constructors

Why should this be improved

flame create adds dev:flame_lint without a version constraint, so games created today get the
stable flame_lint 1.4.4, which doesn't have the lint. As soon as flame_lint 1.5.0 is released
as stable, every new game from basics or example starts with these diagnostics. Since
flutter analyze treats infos as fatal by default, that breaks the promise in the CLI docs that
these games pass flutter analyze right after they are created.

The new flame-best-practices package skill (#4223) also tells agents to use primary constructors
because of this lint, so the templates should match it.

Risks

Primary constructors need Dart 3.13, which flame 2.0 already requires, so the generated games
can't be on an older SDK anyway.

More information

There is no test that runs flutter analyze on the generated templates, which is why this wasn't
caught. One could be added to create_command_test.dart, for example by writing the template
files into a temporary package that resolves flame_lint from the workspace and analyzing it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions