onLoad method

FutureOr<void> onLoad()

Method to perform late initialization of the Game class.

Usually, this method is the main place where you initialize your Game class. This has several advantages over the traditional constructor:

  • this method can be async;
  • it is invoked when the size of the game widget is already known.

The default implementation returns null, indicating that there is no need to await anything. When overriding this method, you have a choice whether to create a regular or async function.

If you need an async onLoad, then make your override return non-nullable Future<void>:

Future<void> onLoad() async {
  // your code here

Alternatively, if your onLoad function doesn't use any awaiting, then you can declare it as a regular method and then return null:

Future<void>? onLoad() {
  // your code here
  return null;

The engine ensures that this method will be called exactly once during the lifetime of the Game instance. Do not call this method manually.


FutureOr<void> onLoad() => null;