你可以使用where方法来过滤modelListcount不为null的元素,并使用toList方法转换成新的List<MyModel>。以下是示例代码:

class MyModel {
  int? count;

  MyModel(this.count);
}

void main() {
  List<MyModel?> modelList = [
    MyModel(1),
    MyModel(null),
    MyModel(3),
    MyModel(null),
    MyModel(5),
  ];

  List<MyModel> filteredList = modelList
      .where((model) => model?.count != null)
      .map((model) => model!)
      .toList();

  print(filteredList);
}

在上述代码中,使用where方法过滤出modelListcount不为null的元素。然后使用map方法将MyModel?类型转换为MyModel类型,并使用toList方法将结果转换为新的List<MyModel>。最后打印输出过滤后的列表filteredList

基于Flutter已知:MyModelint count; ListMyModel modelList; 如果过滤出modelList中count不为null的且转生新的ListMyModel

原文地址: http://www.cveoy.top/t/topic/iRMa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录