Home > Software design >  flutter null stafety migration: the non nullable local variable must be assigned before it can be us
flutter null stafety migration: the non nullable local variable must be assigned before it can be us

Time:06-14

I try to make flutter null safety migration, I have this old code, that worked before null safety, but after upgrade I have some error when there is "null". How can I change this ?

error for bestMove : the non nullable local variable must be assigned before it can be used

errror for return ScoredMove(0, null); :

the argument null can"t be assigned to the parameter Position

Here is the code class ScoredMove { final int score; final Position move;

  const ScoredMove(this.score, this.move);
}


Position _findNextMove(MoveSearchArgs args) {
  ScoredMove bestMove = _performSearchPly(
      args.board, args.player, args.player, args.numPlies - 1);
  return bestMove.move;
}


ScoredMove _performSearchPly(GameBoard board, PieceType scoringPlayer,
    PieceType player, int pliesRemaining) {
  List<Position> availableMoves = board.getMovesForPlayer(player);

  if (availableMoves.isEmpty) {
    return ScoredMove(0, null);
//the argument null can"t be assigned to the parameter Position
  }

  int score = (scoringPlayer == player)
      ? GameBoardScorer.minScore
      : GameBoardScorer.maxScore;
  ScoredMove bestMove;

  for (int i = 0; i < availableMoves.length; i  ) {
    GameBoard newBoard =
    board.updateForMove(availableMoves[i].x, availableMoves[i].y, player);
    if (pliesRemaining > 0 &&
        newBoard.getMovesForPlayer(getOpponent(player)).isNotEmpty) {
      score = _performSearchPly(
          newBoard, scoringPlayer, getOpponent(player), pliesRemaining - 1)
          .score;
    } else if (pliesRemaining > 0 &&
        newBoard.getMovesForPlayer(player).isNotEmpty) {
      // Opponent has no moves; player gets another turn.
      score =
          _performSearchPly(newBoard, scoringPlayer, player, pliesRemaining - 1)
              .score;
    } else {
      score = GameBoardScorer(newBoard).getScore(scoringPlayer);
    }
//bestMove  error the non nullable local variable must be assigned before it can be used
    if (bestMove == null ||
        (score > bestMove.score && scoringPlayer == player) ||
        (score < bestMove.score && scoringPlayer != player)) {
      bestMove =
          ScoredMove(score, Position(availableMoves[i].x, availableMoves[i].y));
    }
  }

  return bestMove;
}

class MoveFinder {
  final GameBoard initialBoard;

  MoveFinder(this.initialBoard) : assert(initialBoard != null);


  Future<Position> findNextMove(PieceType player, int numPlies) {
    return compute(
      _findNextMove,
      MoveSearchArgs(
        board: this.initialBoard,
        player: player,
        numPlies: numPlies,
      ),
    );
  }
}

CodePudding user response:

You can't do

ScoredMove(0, null)

Because the move parameter is not allowed to be null. A possible solution is to make it nullable. You can do that by adding ? after the type, like

class ScoredMove { final int score; final Position? move;
  const ScoredMove(this.score, this.move);
}

And for the other error you can try changing

ScoredMove bestMove;

to

ScoredMove? bestMove = null;

and also changing

return bestMove;

to

return bestMove!;
  • Related