flutter 无法将'Null'型别的值指派给'double'型别的变数

j8ag8udp  于 2022-11-30  发布在  Flutter
关注(0)|答案(2)|浏览(199)

你好,我有一个连接四个游戏谁之前工作的空安全,但我试图使迁移,但我有一个问题,scores[i] = null;我不能这样写,但没有它,我有一个冻结时,CPU是肯定要松散

int _compute(Board board, int step, int deepness, List<double> scores) {
    for (var i = 0; i < 7; ++i) {
      final boardCopy = board.clone();

      final target = boardCopy.getColumnTarget(i);
      if (target == -1) {
        scores[i] = null;  // <<<---- HERE I CAN'T USE NULL
        continue;
      }

      final coordinate = Coordinate(i, target);

      boardCopy.setBox(coordinate, player);
      if (boardCopy.checkWinner(coordinate, player)) {
        scores[i] += deepness / (step + 1); 
        continue;
      }

      for (var j = 0; j < 7; ++j) {
        final target = boardCopy.getColumnTarget(j);
        if (target == -1) {
          continue;
        }

        final coordinate = Coordinate(j, target);

        boardCopy.setBox(coordinate, otherPlayer);
        if (boardCopy.checkWinner(coordinate, otherPlayer)) {
          scores[i] -= deepness / (step + 1); 
          continue;
        }

        if (step + 1 < deepness) {
          _compute(board, step + 1, deepness, scores);
        }
      }
    }

    return _getBestScoreIndex(scores);
  }

  int _getBestScoreIndex(List<double> scores) {
    int bestScoreIndex = scores.indexWhere((s) => s != null);
    scores.asMap().forEach((index, score) {
      if (score != null &&
          (score > scores[bestScoreIndex] ||
              (score == scores[bestScoreIndex] && _random.nextBool()))) {
        bestScoreIndex = index;
      }
    });
    return bestScoreIndex;
  }

如果我使用List<double?>

int _compute(Board board, int step, int deepness, List<double?> scores) {
    for (var i = 0; i < 7; ++i) {
      final boardCopy = board.clone();

      final target = boardCopy.getColumnTarget(i);
      if (target == -1) {
        scores[i] = null;  
        continue;
      }

      final coordinate = Coordinate(i, target);

      boardCopy.setBox(coordinate, player);
      if (boardCopy.checkWinner(coordinate, player)) {
        scores[i] += deepness / (step + 1);//<<<---- HERE I CAN'T USE +=
        continue;
      }

      for (var j = 0; j < 7; ++j) {
        final target = boardCopy.getColumnTarget(j);
        if (target == -1) {
          continue;
        }

        final coordinate = Coordinate(j, target);

        boardCopy.setBox(coordinate, otherPlayer);
        if (boardCopy.checkWinner(coordinate, otherPlayer)) {
          scores[i] -= deepness / (step + 1); //<<<---- HERE I CAN'T USE -=
          continue;
        }

        if (step + 1 < deepness) {
          _compute(board, step + 1, deepness, scores);
        }
      }
    }

    return _getBestScoreIndex(scores);
  }

  int _getBestScoreIndex(List<double?> scores) {
    int bestScoreIndex = scores.indexWhere((s) => s != null);
    scores.asMap().forEach((index, score) {
      if (score != null && // <<<---- HERE I CAN'T USE score !=
          (score > scores[bestScoreIndex] ||  // <<<---- HERE I CAN'T USE score >
              (score == scores[bestScoreIndex] && _random.nextBool()))) {
        bestScoreIndex = index;
      }
    });
    return bestScoreIndex;
  }
tjjdgumg

tjjdgumg1#

您的代码问题在于,在函数定义中,您将score变量的数据类型定义为List<double>。因此,在赋值score[i] = null时出现错误。要修复此问题,请将score的数据类型定义为List<double?>

1qczuiv0

1qczuiv02#

我相信参数scores是一个类型定义的值,它有一个double,这是一个非空值。所以你不能把null赋给double,因为它有赋值的类型。
您可以将其作为可选值,如List<double?>。但这样一来,列表中的值将是可选的,您需要在使用它们之前强制展开或执行空值检查。

相关问题