return 类型 'String?' 不是闭包上下文所要求的 'bool'

The return type 'String?' isn't a 'bool', as required by the closure's context

我想将这个国家代码添加到 DropdownButton

Country is my data class.

DropdownButton<Country> androdDropDown(BuildContext context) {
    List<DropdownMenuItem<Country>> dropDownItems = [];

    List countryList =
        getAppState(context).counties.where((i) => i.code).toList();

    return DropdownButton<Country>(
      value: countryList.reversed.first,
      items: dropDownItems,
      onChanged: (value) {},
    );
  }

错误发生在i.code

行中:

List countryList =
    getAppState(context).counties.where((i) => i.code).toList();

您正在尝试过滤代码等于 true 的国家/地区。但是可变代码不是布尔值而是字符串?。尝试将其替换为:

List countryList =
    getAppState(context).counties.where((i) => i.code != null).toList();