Ad

How To Select Item In List Where It Has Spesific Value Like True/false

- 1 answer

i made list of checkbox with the data from dummy List it contains 'value', 'label', 'image_url' i want to get the item that has true in the value and use it for some purpose not all item index.

List optionList = [
    {
      'value': false,
      'label': 'Walking/Bicycle',
      'image_url':
          'https://images.unsplash.com/photo-1608393508049-40db8711fbd1?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=870&q=80',
    },
    {
      'value': true,
      'label': 'Motorcycle',
      'image_url':
          'https://images.unsplash.com/photo-1591637333184-19aa84b3e01f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=387&q=80',
    },
  ];

this is the widget

optionList
                .map(
                  (e) => Container(
                    margin: EdgeInsets.fromLTRB(0, 12, 0, 0),
                    child: Row(
                      children: [
                        // Check Box
                        Container(
                          child: Checkbox(
                            value: e['value'],
                            onChanged: (value) {
                              setState(() {
                                e['value'] = value;
                              });
                            },
                          ),
                        ),
                        // Image
                     ... too much code
                        // Text
                     ... too much code
                      ],
                    ),
                  ),
                )
                .toList(),
Ad

Answer

you can try this to filter your selected data.

final selectedItemsArr = optionList.where((element) => (element.value ?? false == true)).toList(); //array of only selected items
Ad
source: stackoverflow.com
Ad