Home > Software engineering >  No Such Method Error: Class List<Category> has no instance getter 'entries' Receiver
No Such Method Error: Class List<Category> has no instance getter 'entries' Receiver

Time:06-06

thats my all code i think i got error when i writing get SelecctedCategories fonction but i didnt solve it

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);}
  

i cant find the problem can someone help me i shared my code.i am waiting your answers

CodePudding user response:

You have a typo in the line

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

UPDATE:
That's because your selectedCategories getter reads from the other getter categories and the getter categories return a list<Category> not a 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();}

to solve this, simply change your selectedCategories to read from the map

get selectedCategories => _categories.entries
//instead of 
get selectedCategories => categories.entries
  • Related