Search code examples
flutterdropdownprovider

Flutter : How to add dynamic list to Dropdown ? Edited #2


I have list received from previous page and want to put it in Dropdown menu ,the list is contain many items :

List myItems = [];
getItem() {
  categories.forEach((item) {
    print(item.name); //----------the print result is correct
    myItems.add(item.name);
  });
  return myItems;
}

I put it thus in Dropdown menu :

 DropdownButton(
                        onChanged: (v) {
                          _selected = prod.get_cat(v);
                        },
                        elevation: 16,
                        style: TextStyle(
                            color: Colors.black,
                            fontWeight: FontWeight.bold,
                            fontSize: 22),
                        underline: SizedBox(),
                        value: _selected,
                        items:
                            getItem().map<DropdownMenuItem<String>>((value) {
                          return DropdownMenuItem<String>(
                            value: value[id],
                            child: Center(child: Text(value[name])),
                          );
                        }).toList(),
                      )

the value change in provider :

get_cat(id) {
    _selectedValue = id;
    return _selectedValue;
  }

I get error :

A non-null String must be provided to a Text widget. 'package:flutter/src/widgets/text.dart': Failed assertion: line 370 pos 10: 'data != null'

How can I solve this?


Solution

  • The identifiers should be strings.

    value: value['id'],
    child: Center(child: Text(value['name'])),