Search code examples
flutterasynchronousflame

The argument type 'List<Future<Sprite>>' can't be assigned to the parameter type 'List<Sprite>'


class MyGame extends BaseGame with HasTapableComponents {
  SpriteAnimationComponent girl = SpriteAnimationComponent();

  MyGame();
  @override
  Future<void> onLoad() async {
    final sprites = [0, 1, 2,3,4,5,6,7,8,9]
      .map((i) async => await Sprite.load('Attack__00$i.png'))
      .toList();
    girl = SpriteAnimationComponent(
      animation: SpriteAnimation.spriteList(sprites, stepTime: 0.01),
      size: Vector2.all(100) 
    );
    add(girl);
    print(size);
  }
}

Following the github documents for flutter flame for the implementation of SpriteAnimationComponent, animation: SpriteAnimation.spriteList(sprites, ...). The issue here as I've noticed is that the sprites are a Future list of sprites whereas spriteList requires a list of Sprites. Is this an issue in the docs, or am I going wrong somewhere?


Solution

  • You have to wait for the list of future sprites to materialize, like this:

    final sprites = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
          .map((i) => Sprite.load('Attack__00$i.png'));
    final animation = SpriteAnimation.spriteList(
      await Future.wait(sprites),
      stepTime: 0.01,
    );
    girl = SpriteAnimationComponent(
      animation: animation,
      size: Vector2.all(100) 
    );
    add(girl);
    

    Edit: I can see that it is wrong in the docs, I'll update them.