The argument type 'Object?' can't be assigned to the parameter type 'String'.dartargument_type_not_assignable
code
bold
italic
class Category{
final String name;
final int numOfCoure;
final String image;
Category(this.name, this.numOfCoure, this.image);
}
var categoriesData = [
{"name":"Lúa", 'course': 17, 'image' :"data\\images\\lua.png"},
{"name":"Ngô", 'course': 25, 'image' :"data\\images\\ngo.png"},
{"name":"Rau", 'course': 13, 'image' :"data\\images\\rau.png"},
{"name":"Hoa", 'course': 17, 'image' :"data\\images\\hoa.png"},
];
List<Category> categories = categoriesData
.map((item) => Category(item['name'], item['course'], item['image']))
.toList();
CodePudding user response:
Use named parameter and it will solve your problem.
class Category{
final String? name;
final int? numOfCoure;
final String? image;
Category({this.name, this.numOfCoure, this.image});
}
var categoriesData = [
{"name":"Lúa", 'course': 17, 'image' :"data\\images\\lua.png"},
{"name":"Ngô", 'course': 25, 'image' :"data\\images\\ngo.png"},
{"name":"Rau", 'course': 13, 'image' :"data\\images\\rau.png"},
{"name":"Hoa", 'course': 17, 'image' :"data\\images\\hoa.png"},
];
List<Category> categories = categoriesData
.map((item) => Category(
name: item['name'],
numOfCoure: item['course'],
image: item['image'],
))
.toList();
Make sure to add nullable character in front of the field declared in Category class as you haven't initialized them in constructor.
CodePudding user response:
Try giving it a type
class Category{
final String name;
final int numOfCoure;
final String image;
Category(this.name, this.numOfCoure, this.image);
}
var categoriesData = [
{"name":"Lúa", 'course': 17, 'image' :"data\\images\\lua.png"},
{"name":"Ngô", 'course': 25, 'image' :"data\\images\\ngo.png"},
{"name":"Rau", 'course': 13, 'image' :"data\\images\\rau.png"},
{"name":"Hoa", 'course': 17, 'image' :"data\\images\\hoa.png"},
];
List<Category> categories = categoriesData
.map((item) => Category(item['name'] as String, item['course'] as int, item['image'] as String))
.toList();