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

问题描述 投票:0回答:2

我是Flutter的新手,请帮助我。

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

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);
    }
json flutter dart error-handling flutter-layout
2个回答
0
投票

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

如果有任何疑问,可以查看here处的文档。

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

1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.