错误:无法将参数类型“ int”分配给参数类型“ String”

时间:2020-03-09 14:41:42

标签: json flutter dart error-handling flutter-layout

我想在此代码中使用索引,但在代码中给我错误

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

代码

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

声明如下

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

2 个答案:

答案 0 :(得分:1)

作为@JosepBové答案的补充,Effective Dart文档说使用String插值更为可取。来源prefer-using-interpolation-to-compose-strings-and-values。所以你可以做Text('${user.index}')

答案 1 :(得分:0)

您不能将整数发送到String类型的参数,因此基本上您只需要使用toString方法将整数转换为字符串。

如有任何疑问,请查看here上的文档。

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