这是我所有的代码,我认为我得到了错误,当我写得到selectedcategories函数,但我没有解决它
Widget build(BuildContext context) {
return Flexible(
child: Obx(
() => ListView.builder(
itemCount: controller.selectedCategories.length,
itemBuilder: (_, index) {
return CategoryWidget(
category: controller.selectedCategories[index],
);
},
),
),
);
var _categories = {
Category("Aksiyon", Color(0xFF21d0d0)): false,
Category("Bilimkurgu", Color(0xFF21d0d0)): false,
Category("Suç", Color(0xFF21d0d0)): false,
Category("Macera", Color(0xFF21d0d0)): false,
Category("Drama", Color(0xFF21d0d0)): false,
}.obs;
void toggle(Category item){_categories [item]= !(_categories[item]?? true};
print(_categories[item]);
}
get selectedCategories => categories.entries
.where((element) => element.value).map((e) => e.key)
.toList();
get categories => _categories.entries.map((e) => e.key).toList();}
class Category{
final String name;
final Color color;
Category(this.name,this.color);}
我找不到问题,有人能帮助我,我分享我的代码。我在等你的回答
你这一行打错了
void toggle(Category item){_categories [item]= !(_categories[item]?? true};
print(_categories[item]);
}
!(_categories[item]?? true};
//Change to
!(_categories[item]?? true);
//The curly brackets after the semicolon
更新:
这是因为你的selectedCategories
getter从另一个gettercategories
读取getter
类别返回list<Category>
而不是Map<Category, bool>
get selectedCategories => categories.entries
.where((element) => element.value).map((e) => e.key)
.toList();
get categories => _categories.entries.map((e) => e.key).toList();}
要解决这个问题,只需将selectedCategories
更改为读取地图
get selectedCategories => _categories.entries
//instead of
get selectedCategories => categories.entries