Home > database >  How to select item in list where it has spesific value like true/false
How to select item in list where it has spesific value like true/false

Time:02-21

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(),

CodePudding user response:

you can try this to filter your selected data.

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

CodePudding user response:

You can use this

optionList.map((e){
   if(e["value"]){
     print(e)
     // Here you can do whatever you want with values that are true.
   }
   // Add your UI logic here
},).toList(),

I have removed your UI part, and converted your arrow function "=>" to a block body in map((e) => something) to map((e){do something plus UI}).

  • Related