[Solved] The argument type 'Object?' can't be assigned to the parameter type 'String'.?

Question

Asked by Muhammad B on January 05, 2022 (source).

I have a problem with models flutter project that I have.. I get an error:

The argument type 'Object?' can't be assigned to the parameter type 'String'.

The argument type 'Object?' can't be assigned to the parameter type 'Int'.

The argument type 'Object?' can't be assigned to the parameter type 'String'.

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"},
];

error in this part

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

thanks for the answers..

Answer

Question answered by h8moss (source).

Dart doesn't know what categoriesData['name'], categoriesData['courses'] or categoriesData['image'] are supposed to be, to tell it, you can use the as keyword:

categories = categoriesData
    .map((item) => Category(item['name'] as String, item['courses'] as int, item['image'] as String))
    .toList();
DART FLUTTER LIST MODEL
SHARE: