【问题标题】:Error 'A value of type 'dynamic' can't be assigned to a variable of type 'String'.' in Dart 2.2错误“无法将“动态”类型的值分配给“字符串”类型的变量。在飞镖 2.2
【发布时间】:2019-07-29 00:53:30
【问题描述】:

自上次飞镖更新 (2.2) 我收到了这个错误,

''动态'类型的值不能分配给类型的变量 '字符串'。'

这对我来说没有多大意义。 代码绝对是微不足道的:

    class EmployeeMirror {
  EmployeeMirror(this.id, this.name);

  EmployeeMirror.fromMap(Map<String, dynamic> _map) {
    id = _map['id'];      // error here
    name = _map['name'];  // and here
  }

  int id;
  String name;
}

我认为不相关,但这是在 Aqueduct 项目中。

提前感谢您的帮助

【问题讨论】:

  • 您可以通过从analysis.options.yaml 文件中删除implicit-cast: false(或将其设置为true)来禁用此检查。 dartlang.org/guides/language/…
  • 我有同样的问题,它与渡槽有关

标签: string dynamic dart aqueduct


【解决方案1】:
class EmployeeMirror {
  EmployeeMirror(this.id, this.name);

  EmployeeMirror.fromMap(Map<String, dynamic> _map) {
    id = _map['id'] as int;
    name = _map['name'] as String;
  }

  int id;
  String name;
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 2020-01-21
  • 2021-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
相关资源
最近更新 更多