flame-engine/flame

NoSuchMethodError for enum.name within FlameGame Lifecycle

Closed this issue · 1 comments

What happened?

When using the .name getter on an enum to add an overlay within a FlameGame's onLoad or related methods, a NoSuchMethodError is thrown at runtime. This occurs despite the project using a modern Dart SDK (>=2.15) where the .name getter is fully supported.

Crucially, calling the same enum.name getter outside of the FlameGame context (e.g., in main.dart before runApp) works as expected, printing the correct string value. This strongly suggests a build toolchain or compilation context issue specific to how FlameGame is initialized, rather than a general language feature bug.

The development environment has been verified to be up-to-date and correctly configured:

Flutter Version: 3.32.4 (channel stable)

Dart SDK Version: 3.8.1

Flame Version: 1.29.0

pubspec.yaml Environment: sdk: ^3.8.1

IDE Configuration: Both the command line and the IDE (Android Studio) are confirmed to be using the same, correct SDK path.

Standard troubleshooting steps, including flutter clean, restarting the IDE, and even manually deleting build and .dart_tool directories, do not resolve the issue.

What do you expect?

I expect that using the inbuilt .name property of enum will not create a runtime NoSuchMethodError.

How can we reproduce this?

A minimal reproducible example can be created with the following file:

lib/main.dart

import 'package:flame/game.dart';
import 'package:flutter/material.dart';

// 1. Define a simple enum for the overlay state.
enum GameOverlayKey { Title }

// 2. Define the game class.
class MyGame extends FlameGame {
  @override
  Future<void> onLoad() async {
    await super.onLoad();
    // 4. This line will cause the runtime error.
    overlays.add(GameOverlayKey.Title.name);
  }
}

void main() {
  // 3. This proves the Dart environment itself is working correctly.
  // It will print "main.dart: GameOverlayKey.Title.name: Title"
  print('main.dart: GameOverlayKey.Title.name: ${GameOverlayKey.Title.name}');

  runApp(
    GameWidget(
      game: MyGame(),
      overlayBuilderMap: {
        // Using the enum.name here works correctly.
        GameOverlayKey.Title.name: (context, game) {
          return const Center(
            child: Text(
              'Title Overlay',
              style: TextStyle(color: Colors.white, fontSize: 32),
            ),
          );
        },
      },
    ),
  );
}

The error seems to have gone away after a computer restart. I do not know why but I am closing the issue.