英文:
The parameter 'price' can't have a value of 'null' because of its type, but the implicit default value is 'null'
问题
以下是您要翻译的内容:
> 参数 'price' 不能具有 'null' 的值,因为它的类型不允许,但隐式默认值是 'null'。
尝试添加显式的非 'null' 默认值或 'required' 修饰符。dartmissing_default_value_for_parameter
{num price}
class Item {
final String id;
final String name;
final String desc;
final String color;
final String image;
final num price;
Item({this.id, this.name, this.desc, this.color, this.image, this.price});
}
final products = [
Item(
id: "Codepur001",
name: "iPhone 12 Pro",
desc: "Apple iPhone 12 代",
price: 999,
color: "#33505a",
image: "https://www.pngmart.com/files/15/Apple-iPhone-12-PNG-HD.png")
];
英文:
Note I Do not want to add required, I am looking for other solution.
>The parameter 'price' can't have a value of 'null' because of its type, but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.dartmissing_default_value_for_parameter
{num price}
class Item {
final String id;
final String name;
final String desc;
final String color;
final String image;
final num price;
Item({this.id, this.name, this.desc, this.color, this.image, this.price});
}
final products = [
Item(
id: "Codepur001",
name: "iPhone 12 Pro",
desc: "Apple iPhone 12th generation",
price: 999,
color: "#33505a",
image: "https://www.pngmart.com/files/15/Apple-iPhone-12-PNG-HD.png")
];
答案1
得分: 1
因为类中的变量是非空的,您应该将它们设置为可空或者在构造函数中设置为必需,如下所示:
将它们设为必需
class Item {
final String id;
final String name;
final String desc;
final String color;
final String image;
final num price;
Item({
required this.id,
required this.name,
required this.desc,
required this.color,
required this.image,
required this.price,
});
}
将它们设为可空
class Item {
final String? id;
final String? name;
final String? desc;
final String? color;
final String? image;
final num? price;
Item({
this.id,
this.name,
this.desc,
this.color,
this.image,
this.price,
});
}
或者,您可以使用默认值,而不是使用 required
,如下所示:
class Item {
final String id;
final String name;
final String desc;
final String color;
final String image;
final num price;
Item({
this.id = '',
this.name = '',
this.desc = '',
this.color = '',
this.image = '',
this.price = 0,
});
}
英文:
Because the variables in the class are non-null, you should either make them nullable or make them required compulsory from the constructor like so:
Making them required
class Item {
final String id;
final String name;
final String desc;
final String color;
final String image;
final num price;
Item({
required this.id,
required this.name,
required this.desc,
required this.color,
required this.image,
required this.price,
});
}
Making them nullable
class Item {
final String? id;
final String? name;
final String? desc;
final String? color;
final String? image;
final num? price;
Item({
this.id,
this.name,
this.desc,
this.color,
this.image,
this.price,
});
}
Alternatively, you can assign default values instead of using required
like so:
class Item {
final String id;
final String name;
final String desc;
final String color;
final String image;
final num price;
Item({
this.id = '',
this.name = '',
this.desc = '',
this.color = '',
this.image = '',
this.price = 0,
});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论