Skip to main content

correct-game-instantiating

has auto-fix
pro+

Warns when a game is instantiated in a stateless widget build method.

Instantiating game in a stateless widget build method cases the game to reset every time Flutter rebuilds the widget.

Example

❌ Bad:

class MyGamePage extends StatelessWidget {

Widget build(BuildContext context) {
// LINT: Incorrect game instantiation. The game will reset on each rebuild.
return GameWidget(game: MyFlameGame());
}
}

✅ Good:

class MyGamePage extends StatefulWidget {

State<MyGamePage> createState() => _MyGamePageState();
}

class _MyGamePageState extends State<MyGamePage> {

late final _game = _MyFlameGame();


Widget build(BuildContext context) {
return GameWidget(game: _game);
}
}

or:

class MyGamePage extends StatelessWidget {

Widget build(BuildContext context) {
return GameWidget.controlled(gameFactory: MyFlameGame.new);
}
}