【问题标题】:Error: The argument type 'int' can't be assigned to the parameter type 'String'错误:不能将参数类型“int”分配给参数类型“String”
【发布时间】:2020-06-21 11:29:23
【问题描述】:

我想在这段代码中使用索引,但在代码中出现错误

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


    【解决方案1】:

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

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

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

    【讨论】:

    • 那么您应该将问题标记为已解决,并打勾。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-12
      • 2023-01-23
      • 2021-11-06
      • 2022-10-12
      • 2021-10-02
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多