0

I wanted to create a checklist for the programming languages I know. I have used Set() to contain the data. But when I use it in the CheckboxListTile() it is not checking the box in the UI. I wanted to do it with using flutter_hooks.

  1. I have created an enum for ProgrammingLanguages.

enum ProgrammingLanguages { C, DART, PYTHON, JAVASCRIPT }

  1. Then I have initialised the set in stateless Widget class like this

final _selectedLanguages = useState<Set>(Set<ProgrammingLanguages>());

  1. In the build I have added a CheckBoxListTile like this.
CheckboxListTile(
  title: Text('C'),
  value: _selectedLanguages.value.contains(ProgrammingLanguages.C),
  onChanged: (value) {
     value
      ? _selectedLanguages.value.remove(ProgrammingLanguages.C)
      : _selectedLanguages.value.add(ProgrammingLanguages.C);
  }),

But in the final UI I am not able to activate the check box. Please help.

Praveen117
  • 11
  • 3
  • and where are you calling `Set.add`? `onChanged` takes a `bool` parameter and you have to use it when changing `_selectedLanguages.value` - btw instead of `Set` better use `Map` – pskink Sep 21 '21 at 04:38
  • I didnt use map because I dont want to repeat values. Set contains unique items but map can contain duplicates right. – Praveen117 Sep 21 '21 at 16:52
  • but as you can see the code is much more simple than in `Set` - you dont have to use any `add` / `remove` - you just do `map.value[ProgrammingLanguages.DART] = b;` thats all – pskink Sep 21 '21 at 17:00
  • and if you _really_ want to use `Set`s then try: `class Langs extends HookWidget { @override Widget build(BuildContext context) { final langs = useState>({}); print(langs.value); return Column( children: [ for (var l in ProgrammingLanguages.values) Row( children: [ Checkbox( value: langs.value.contains(l), onChanged: (b) => langs.value = b? langs.value.union({l}) : langs.value.difference({l}), ), Text(l.toString()), ], ), ], ); } }` – pskink Sep 22 '21 at 05:42

0 Answers0