Home > Software design >  The argument type 'List<DropdownMenuItem<String>>' can't be assigned to t
The argument type 'List<DropdownMenuItem<String>>' can't be assigned to t

Time:05-01

List names = [{"Label":"Name 1","Value":"60"},{"Label":"Name 2","Value":"61"},{"Label":"Name 3","Value":"131"},{"Label":"Name 4","Value":"62"},{"Label":"Name 5","Value":"63"}];

I changed String to dynamic, and it returned:

"DropdownMenuItem"

                  DropdownSearch<String>(
                  mode: Mode.BOTTOM_SHEET,
                  items: names.map((item) {
                    return DropdownMenuItem(
                      child: new Text(item['Label'].toString()),
                      value: item['Value'].toString(),
                    );
                  }).toList(),
                  dropdownSearchDecoration: InputDecoration(
                    labelText: "Custom BottomShet mode",
                    contentPadding: EdgeInsets.fromLTRB(12, 12, 0, 0),
                    border: OutlineInputBorder(),
                  ),
                  onChanged: print,
                  showSearchBox: true,
                  searchFieldProps: TextFieldProps(
                    decoration: InputDecoration(
                      border: OutlineInputBorder(),
                      contentPadding: EdgeInsets.fromLTRB(12, 12, 8, 0),
                      labelText: "Select...",
                    ),
                  ),
                  popupTitle: Container(
                    height: 50,
                    decoration: BoxDecoration(
                      color: Theme.of(context).primaryColorDark,
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(20),
                        topRight: Radius.circular(20),
                      ),
                    ),
                    child: Center(
                      child: Text(
                        'Search',
                        style: TextStyle(
                          fontSize: 22,
                          fontWeight: FontWeight.bold,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ),
                  popupShape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(24),
                      topRight: Radius.circular(24),
                    ),
                  ),
                ),

CodePudding user response:

Another way is to use the List.generate function:

    DropdownSearch<String>(
      mode: Mode.BOTTOM_SHEET,
      items: List<String>.generate(
        names.length,
        (index) {
          return names[index]['Value'].toString();
        },
        growable: false,
      ),
      ...

CodePudding user response:

items are DropdownMenuItem type not String.

DropdownSearch<DropdownMenuItem>(
  mode: Mode.BOTTOM_SHEET,
  items: names.map((item) {
    return DropdownMenuItem(
      child: new Text(item['Label'].toString()),
      value: item['Value'].toString(),
    );
  }).toList(),
...
  • Related