Dart开发报错:类型‘Property’未定义getter‘category’及‘subCategory’
解决Property类缺少category和subCategory属性的错误
你遇到的错误是因为当前定义的Property类中不存在category和subCategory这两个属性,所以在筛选方法里尝试访问property.category和property.subCategory时,IDE无法识别这两个成员。
解决方案
需要修改Property类,添加分类属性并更新构造函数,具体步骤如下:
- 更新Property类定义
在类中新增category和subCategory的final字符串字段,并在构造函数中加入对应的必填参数:
class Property { final String name; final String description; final double pricePerMonth; // 新增分类属性 final String category; final String subCategory; Property({ required this.name, required this.description, required this.pricePerMonth, // 构造函数新增分类参数 required this.category, required this.subCategory, }); }
- 确保实例化Property时传入分类值
之后所有创建Property对象的代码,都需要补充传入category和subCategory参数,示例:
final sampleProperty = Property( name: "阳光花园公寓", description: "朝南两居室,配套齐全", pricePerMonth: 3200.0, category: "公寓", subCategory: "两居室", );
- 验证筛选方法
完成上述修改后,_getPropertiesToShow方法中对property.category和property.subCategory的访问将恢复正常,不会再触发报错。
内容的提问来源于stack exchange,提问作者Woke Vibes Hollys
相关产品推荐
相关产品推荐

