flutterdartvisual-studio-codeflame

What exactly is FutureOr<void> onLoad?


Related: How to Make VSCode Autocomplete Future<void> onLoad() Instead of FutureOr<void> onLoad()

When writing onLoad in VSCode, it auto-completes as FutureOr<void> onLoad(). However, most Flutter Flame sample code online uses Future<void> onLoad(), so I’ve been manually writing it without relying on auto-completion.

For example a sample code: https://docs.flame-engine.org/latest/flame/game.html

 @override
 Future<void> onLoad() async {
   sprite = await Sprite.load('crate.png');
 }

What exactly is FutureOr<void> onLoad ? Is there any difference with or without Or?

What I see after auto-completes onLoad:

class FooComponent extends PositionComponent {
  @override
  FutureOr<void> onLoad() {
    // TODO: implement onLoad
    return super.onLoad();
  }
}

enter image description here enter image description here


Solution

  • FutureOr comes from Flutter, you can read the specific documentation about it here.

    When FutureOr<void> is used the return type of the method can either be Future<void>, void or FutureOr<void>. So if you want to have a syncrounous onLoad then you can return void, otherwise use Future<void>.