Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何计数和获取dart和FLATTER中指定值的列表_Flutter_Dart - Fatal编程技术网

Flutter 如何计数和获取dart和FLATTER中指定值的列表

Flutter 如何计数和获取dart和FLATTER中指定值的列表,flutter,dart,Flutter,Dart,我有以下UML类图: 我想显示每家公司所有特色产品的列表视图。 我只希望获得特色产品: 产品00(来自abc公司) 产品02(来自abc公司) 产品11(来自xyz公司) 但是下面的代码只给出了第一项产品00 我试着统计特色产品的数量 但长度始终为1 List<Product> temps; for (var i = 0; i < companies.length; i++) { temps = companies[i]

我有以下UML类图:

我想显示每家公司所有特色产品的列表视图。 我只希望获得特色产品

  • 产品00(来自abc公司)
  • 产品02(来自abc公司)
  • 产品11(来自xyz公司)
但是下面的代码只给出了第一项产品00

我试着统计特色产品的数量 但长度始终为1

List<Product> temps;
    for (var i = 0; i < companies.length; i++) {
      
      temps = companies[i]
          .products
          .where((fp) => fp.isFeatured == true)
          .toList();
    }
    print('===> ${temps.length}'); // <----- here
List temp;
对于(var i=0;ifp.isFeatured==true)
.toList();
}
打印('=>${temps.length}');//_MyHomePageState();
}
类_MyHomePageState扩展状态{
int countFeaturedProducts(){
列出临时工;
对于(var i=0;ifp.isFeatured==true)
.toList();
//本产品是真正的特色产品
用于(临时变量项){
打印(项目名称);
}
}
打印('=>${temps.length}');
打印('=>out');
返回时间长度;
}
列出功能产品(整数索引){
列出所有特色;
对于(var i=0;i<公司[index].products.length;i++){
allFeatured=公司[索引]。产品;
}
返回所有特征;
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
主体:容器(
身高:100,
颜色:颜色。蓝灰色,
子项:ListView.builder(
itemCount:countFeaturedProducts()//
在每个循环中替换
temp
。听起来像是要将特征项附加到聚合列表中。因此,您需要如下内容:

temps.addAll(companies[i]
          .products
          .where((fp) => fp.isFeatured == true)
          .toList());
List<Product> temps = [];
    for (var i = 0; i < companies.length; i++) {
      
      temps.addAll(companies[i]
          .products
          .where((fp) => fp.isFeatured == true));
    }
    print('===> ${temps.length}'); // <----- here
但是直接使用它会失败,因为
temps
没有初始化,所以您需要用如下方式初始化它:

temps.addAll(companies[i]
          .products
          .where((fp) => fp.isFeatured == true)
          .toList());
List<Product> temps = [];
    for (var i = 0; i < companies.length; i++) {
      
      temps.addAll(companies[i]
          .products
          .where((fp) => fp.isFeatured == true));
    }
    print('===> ${temps.length}'); // <----- here
在每个循环中替换
temp
。听起来像是要将特征项附加到聚合列表中。因此,您需要如下内容:

temps.addAll(companies[i]
          .products
          .where((fp) => fp.isFeatured == true)
          .toList());
List<Product> temps = [];
    for (var i = 0; i < companies.length; i++) {
      
      temps.addAll(companies[i]
          .products
          .where((fp) => fp.isFeatured == true));
    }
    print('===> ${temps.length}'); // <----- here
但是直接使用它会失败,因为
temps
没有初始化,所以您需要用如下方式初始化它:

temps.addAll(companies[i]
          .products
          .where((fp) => fp.isFeatured == true)
          .toList());
List<Product> temps = [];
    for (var i = 0; i < companies.length; i++) {
      
      temps.addAll(companies[i]
          .products
          .where((fp) => fp.isFeatured == true));
    }
    print('===> ${temps.length}'); // <----- here

它给出了以下错误:
NoSuchMethodError:null上的无效成员:“addAll”
@fromearth我更新了我的答案,并提供了一种新的方法,这可能更简单。如果这解决了您的问题,请接受它。它给出了以下错误:
NoSuchMethodError:null上的无效成员:“addAll”
@fromearth我也更新了我的答案用一种新的方法来做,可能会更容易。如果这解决了你的问题,请接受它。