H.Park
H.Park

Reputation: 21

Error: The argument type 'int' can't be assigned to the parameter type 'String'

I want to use index in this code but giving me error in the code

Error: The argument type 'int' can't be assigned to the parameter type 'String'.
        user.index,
             ^

Code

final index = Padding(
  padding: EdgeInsets.all(8.0),
  child: Text(
    user.index,
    style: TextStyle(fontSize: 14.0, color: Colors.black),
  ),
);

Declarations are as follows

class User {
      final int index;
      final String about;
      final String name;
      final String email;
      final String picture;
      User(this.index, this.about, this.name, this.email, this.picture);
    }

Upvotes: 0

Views: 7653

Answers (2)

Josep Bové
Josep Bové

Reputation: 688

You can't send a integer to a parameter of type String so basically you just need to cast the integer to a string using the toString method.

You can look at the documentation at here if you have any doubts.

final index = Padding(
  padding: EdgeInsets.all(8.0),
  child: Text('${user.index}'),
    style: TextStyle(fontSize: 14.0, color: Colors.black),
  ),
);

Upvotes: 0

Federick Jonathan
Federick Jonathan

Reputation: 2864

As an addition to @Josep Bové's answer, Effective Dart documentation says using String interpolation is more preferable. Source, prefer-using-interpolation-to-compose-strings-and-values. So you could do Text('${user.index}')

Upvotes: 1

Related Questions