Home > Enterprise >  why Doesn't see List type?
why Doesn't see List type?

Time:08-26

I am using declare an array -

  final List<DropdownMenuItem<String>> listDropDown;

And I use the following construction -

class DropDown extends StatelessWidget {

  final String dropDownValue;
  final List<DropdownMenuItem<String>> listDropDown;

  DropDown(this.dropDownValue, this.listDropDown);

But I get an error -

The argument type 'List<dynamic>' can't be assigned to the parameter type 'List<DropdownMenuItem<String>>?'.

In this place -

return DropdownMenuItem(
                      value: dropDownValue,
                      child: Text(items, style: TextStyle(fontFamily: "Mark-Pro", fontSize: 18, fontWeight: FontWeight.w500, color: configColors.darkBlue),),
                    );
                  }).toList(),

full dropDownButtom -

child: DropdownButton(
                  items: listDropDown.map((String items) {
                    return DropdownMenuItem(
                      value: dropDownValue,
                      child: Text(items, style: TextStyle(fontFamily: "Mark-Pro", fontSize: 18, fontWeight: FontWeight.w500, color: configColors.darkBlue),),
                    );
                  }).toList(),
                  // Initial Value
                  value: dropDownValue,

                  // Down Arrow Icon
                  icon: const Icon(Icons.keyboard_arrow_down),

                  // Array list of items

                  // After selecting the desired option,it will
                  // change button value to selected value
                  onChanged: (String? newValue) {
                    mystate(() {
                      dropDownValue = newValue!;
                    });
                  },
                ),

CodePudding user response:

Once you like to use map on final List<DropdownMenuItem<String>> listDropDown; it will be items: listDropDown.map((DropdownMenuItem items) {.

As for your use case, you don't need to pass List<DropdownMenuItem<String>> , just pass List<String>

class DropDown extends StatelessWidget {
  final String dropDownValue;
  final List<String> listDropDown;

  const DropDown(this.dropDownValue, this.listDropDown);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        DropdownButton<String>(
          items: listDropDown.map((String items) {
            return DropdownMenuItem(
              value: dropDownValue,
              child: Text(
                items,
                style: TextStyle(
                    fontFamily: "Mark-Pro",
                    fontSize: 18,
                    fontWeight: FontWeight.w500,
                    color: configColors.darkBlue),
              ),
            );
          }).toList(),
          // Initial Value
          value: dropDownValue,

CodePudding user response:

The issue is in calling DropDown, you are passing List instead of List<DropdownMenuItem> to DropDown's listDropDown.

try this:

DropdownButton<String>(
                  items: listDropDown.map((String items) {
                    return DropdownMenuItem<String>(
                      value: dropDownValue,
                      child: Text(items, style: TextStyle(fontFamily: "Mark-Pro", fontSize: 18, fontWeight: FontWeight.w500, color: configColors.darkBlue),),
                    );
                  }).toList(),
                  // Initial Value
                  value: dropDownValue,

                  
                  icon: const Icon(Icons.keyboard_arrow_down),
                  onChanged: (String? newValue) {
                    mystate(() {
                      dropDownValue = newValue!;
                    });
                  },
                )
  • Related