Dart - How to concatenate a String and my integer

Viewed 4565

How can I concatenate my String and the int in the lines: print('Computer is moving to ' + (i + 1)); and print("Computer is moving to " + (i + 1));

I cant figure it out because the error keeps saying "The argument type 'int' can't be assigned to the parameter type 'String'

void getComputerMove() {
    int move;

    // First see if there's a move O can make to win
    for (int i = 0; i < boardSize; i++) {
      if (_mBoard[i] != humanPlayer && _mBoard[i] != computerPlayer) {
        String curr = _mBoard[i];
        _mBoard[i] = computerPlayer;
        if (checkWinner() == 3) {
          print('Computer is moving to ' + (i + 1));
          return;
        } else
          _mBoard[i] = curr;
      }
    }

    // See if there's a move O can make to block X from winning
    for (int i = 0; i < boardSize; i++) {
      if (_mBoard[i] != humanPlayer && _mBoard[i] != computerPlayer) {
        String curr = _mBoard[i]; // Save the current number
        _mBoard[i] = humanPlayer;
        if (checkWinner() == 2) {
          _mBoard[i] = computerPlayer;
          print("Computer is moving to " + (i + 1));
          return;
        } else
          _mBoard[i] = curr;
      }
    }
  }
2 Answers

With string interpolation:

print("Computer is moving to ${i + 1}"); 

Or just call toString():

print("Computer is moving to " + (i + 1).toString()); 

You can simply use .toString which will convert your integer to String :

void main(){
     
    String str1 = 'Welcome to Matrix number ';
    int n = 24;
     
    //concatenate str1 and n
    String result = str1 + n.toString();
     
    print(result);
}

And in your case it's gonna be like this :

print("Computer is moving to " + (i + 1).toString()); 
Related