dbm1211
dbm1211

Reputation: 61

Dart - How to concatenate a String and my integer

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

Upvotes: 6

Views: 12464

Answers (3)

yassine menssi
yassine menssi

Reputation: 373

 var intValue = Random().nextInt(5)+1;   // 1 <--> 5
 var string = "the nb is $intValue random nb ";

Upvotes: 0

ParSa
ParSa

Reputation: 1426

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()); 

Upvotes: 3

hnnngwdlch
hnnngwdlch

Reputation: 3041

With string interpolation:

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

Or just call toString():

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

Upvotes: 16

Related Questions