PHP Pharaoh
PHP Pharaoh

Reputation: 49

Mapping Lists from 2 lists flutter

sample of Options that i have: sample 2

I have 2 List, the first one is named options:

List<String> _options = [
    'Arts & entertainment',
    'Biographies & memoirs',
    ...
  ];

and the other name _isOptionSelected:

List<bool> _isOptionSelected = [
    false,
    false,
    ..
  ];

im trying to generat a map form those 2 list.

what i have tryed is this:

 Map<String, Map<String, dynamic>> _isOptionMap = {
      'Arts & entertainment': {
        'optionName': 'Arts & entertainment',
        'isOptionSelected': false,
      },
      'Biographies & memoirs': {
        'optionName': 'Biographies & memoirs',
        'isOptionSelected': false,
      },

      ..
    };

and then print it:

for (int i = 0; i < _isChoiceChipSelected.length; i++) {
      _isOptionMap[_options[i]]['isOptionSelected'].update(
          _options[i],
          (value) => _isOptionMap[_options[i]]
              ['isOptionSelected']);
    }
    print(_isOptionMap);

Is a logical problem? and what im I doing wong ?

Upvotes: 0

Views: 380

Answers (1)

camillo777
camillo777

Reputation: 2367

Hello this code creates a map like the one you want.

Be aware that "_options" and "_isOptionSelected" lists must have the same lenght.

void main() {
  List<String> _options = [
    'Arts & entertainment',
    'Biographies & memoirs',
  ];
  
  List<bool> _isOptionSelected = [
    false,
    false,
  ];
  
  Map m = {};
  
  _options.asMap().entries.forEach((entry) {
    int idx = entry.key;
    String val = entry.value;
    
    Map m1 = {
      'optionName': val,
      'isOptionSelected': _isOptionSelected[idx]
    };
    
    m[val] = m1;
  });
    
  print( m );
}

Upvotes: 1

Related Questions