Flutter update is giving me this error: The method '*' was called on null

Flutter update is giving me this error: The method '*' was called on null

我有一个使用 flame 库的 flutter 应用程序。我试图让一个物体在颤振游戏中移动。当我 运行 update 函数时,出现以下错误:

The method '*' was called on null.
Receiver: null
Tried calling: *(0.0)

似乎有些东西没有初始化,update 函数是 运行 之前有东西被初始化。当我注释掉 player.update(t) 时它起作用了,但是更新函数没有被调用。我做错了什么广告我该如何解决?这是我的代码:

游戏控制器Class

class GameController extends Game {
  Size screenSize;
  Player player;

  GameController() {
    initialize();
  }

  void initialize() async {
    final initDimetion = await Flame.util.initialDimensions();
    resize(initDimetion);
    player = Player(this);
  }

  void render(Canvas c) {
    Rect bgRect = Rect.fromLTWH(0, 0, screenSize.width, screenSize.height);
    Paint bgPaint = Paint()..color = Color(0xFFFAFAFA);
    c.drawRect(bgRect, bgPaint);

    player.render(c);
  }

  void update(double t) {
    if (player is Player) { // Tried adding this if statement but it didn't work
      player.update(t);
    }
  }

  void resize(Size size) {
    screenSize = size;
  }
}

玩家Class

class Player {
  final GameController gameController;
  Rect playerRect;
  double speed;

  Player(this.gameController) {
    final size = 40.0;

    playerRect = Rect.fromLTWH(gameController.screenSize.width / 2 - size / 2,
        gameController.screenSize.height / 2 - size / 2, size, size);
  }

  void render(Canvas c) {
    Paint color = Paint()..color = Color(0xFF0000FF);
    c.drawRect(playerRect, color);
  }

  void update(double t) {
    double stepDistance = speed * t;
    Offset stepToSide = Offset.fromDirection(90, stepDistance);
    playerRect = playerRect.shift(stepToSide);
  }
}

您从未将 Player 的速度属性初始化为一个值。因此 Player.update 中的 speed * t 会导致此错误。

在构造函数中简单初始化speed属性

  Player(this.gameController) {
    final size = 40.0;
    this.speed = 0;
    playerRect = Rect.fromLTWH(gameController.screenSize.width / 2 - size / 2,
        gameController.screenSize.height / 2 - size / 2, size, size);
  }