Home > OS >  A value of type 'Null' can't be assigned to a variable of type 'double'
A value of type 'Null' can't be assigned to a variable of type 'double'

Time:11-28

Hello I have a connect four game who worked before null safety but I try to make the migration, but I have a problem with scores[i] = null; I can't write like that but without it I have a freeze when CPU is certain to loose

  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;
  }

if I use 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;
  }

CodePudding user response:

The issue with your code is that in the function definition you have defined the data type of score variable as List<double>. Due to which you get an error on assigning score[i] = null. To fix this use data type of score as List<double?>

CodePudding user response:

i believe the param scores is a type-defined value and it has a double, which is a non null value. So you can't assign null to double as it has the type of value assigned.

You can make it double as an optional value, like a List<double?>. but with this, the values in the list will be optional and you need to force unwrap or do null check before using them.

  • Related