【问题标题】:The argument type 'Object?' can't be assigned to the parameter type 'String'.?参数类型“对象?”不能分配给参数类型“字符串”。?
【发布时间】:2022-01-05 04:53:24
【问题描述】:

我的模型颤振项目有问题.. 我收到一个错误:

参数类型“对象?”不能分配给参数类型 '字符串'。

参数类型“对象?”不能分配给参数类型 '诠释'。

参数类型“对象?”不能分配给参数类型 '字符串'。

class Category {
  final String name;
  final int numOfCourses;
  final String image;

  Category(this.name, this.numOfCourses, this.image);
}

List<Category> categories = categoriesData
    .map((item) => Category(item['name'], item['courses'], item['image']))
    .toList();

var categoriesData = [
  {"name": "Marketing", 'courses': 17, 'image': "assets/images/marketing.png"},
  {"name": "UX Design", 'courses': 25, 'image': "assets/images/ux_design.png"},
  {
    "name": "Photography",
    'courses': 13,
    'image': "assets/images/photography.png"
  },
  {"name": "Business", 'courses': 17, 'image': "assets/images/business.png"},
];

这部分有错误

(item['name'], item['courses'], item['image'])

感谢您的回答..

【问题讨论】:

  • 不完全确定,但有两件事从我的脑海中浮现出来 - 1. 'courses':应该是“courses”:在你的 Json 中?另外,2. 也许 item[] 变量需要作为 item['name'] 传递! (末尾有感叹号)为空安全? (第2点可能不对....)

标签: list flutter dart model


【解决方案1】:

Dart 不知道 categoriesData['name']categoriesData['courses']categoriesData['image'] 应该是什么,要告诉它,你可以使用 as 关键字:

categories = categoriesData
    .map((item) => Category(item['name'] as String, item['courses'] as int, item['image'] as String))
    .toList();

【讨论】:

    猜你喜欢
    • 2021-07-17
    • 2021-12-14
    • 1970-01-01
    • 2021-09-03
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2021-10-13
    相关资源
    最近更新 更多