Home > Net >  List<DropdownMenuItem<String>> is deprecated
List<DropdownMenuItem<String>> is deprecated

Time:11-09

I'm getting this error when I try to reiterate over a DropdownMenuItem

  List<DropdownMenuItem<String>> list = new List();

  _powers.forEach((power) {
    list.add(DropdownMenuItem(
      child: Text(power),
      value: power,
    ));
  });

  return list;
}

CodePudding user response:

use [] to declare empty List

List<DropdownMenuItem<String>> list = []; // change here

  _powers.forEach((power) {
    list.add(DropdownMenuItem(
      child: Text(power),
      value: power,
    ));
  });

  return list;
}

CodePudding user response:

According to the documentation:

The default ‘List’ constructor isn’t available when null safety is enabled.

So, in your case where you don't care about the list size you should use the list literal []:

 List<DropdownMenuItem<String>> list = [];

Other suggestions include using the other List constructors, which can be seen here.

  • Related