我有以下UML类图: UML class diagram
我想显示每个公司中发现的所有特色产品的列表视图。 我预计只会获得精选产品:
,但是下面的代码仅给我第一项产品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
这是带有数据的模型 Dart 代码:
class Product {
final int id;
final String name;
final double price;
final bool isFeatured;
Product({
this.id,
this.name,
this.price,
this.isFeatured,
});
}
class Company {
final int id;
final String name;
final List<Product> products;
Company({
this.id,
this.name,
this.products,
});
}
List<Product> productsOfCompanyAbc = [
Product(
id: 0,
name: 'product 00',
price: 10.0,
isFeatured: true,
),
Product(
id: 1,
name: 'product 01',
price: 20.0,
isFeatured: false,
),
Product(
id: 2,
name: 'product 02',
price: 25.0,
isFeatured: true,
),
];
List<Product> productsOfCompanyXyz = [
Product(
id: 0,
name: 'product 11',
price: 30.0,
isFeatured: true,
),
Product(
id: 1,
name: 'product 12',
price: 40.0,
isFeatured: false,
),
];
List<Company> companies = [
Company(
id: 0,
name: 'Company abc',
products: productsOfCompanyAbc,
),
Company(
id: 1,
name: 'Company xyz',
products: productsOfCompanyXyz,
)
];
这是 Flutter 示例:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int countFeaturedProducts() {
List<Product> temps;
for (var i = 0; i < companies.length; i++) {
temps = companies[i]
.products
.where((fp) => fp.isFeatured == true)
.toList();
// this product TRUE items that is featured
for (var item in temps) {
print(item.name);
}
}
print('===> ${temps.length}');
print('===> out');
return temps.length;
}
List<Product> featuredProducts(int index){
List<Product> allFeatured;
for (var i = 0; i < companies[index].products.length; i++) {
allFeatured = companies[index].products;
}
return allFeatured;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: 100,
color: Colors.blueGrey,
child: ListView.builder(
itemCount: countFeaturedProducts(), //<------- Here give length 1.
itemBuilder: (context, index) {
return ListTile(
title: Text(featuredProducts(index)[index].name), //<------- Here give only one Product 00.
);
},
),
),
);
}
}
答案 0 :(得分:0)
temps = companies[i]
.products
.where((fp) => fp.isFeatured == true)
.toList();
在每个循环中替换temps
。听起来您想将特色商品添加到汇总列表中。所以你想要这样的东西:
temps.addAll(companies[i]
.products
.where((fp) => fp.isFeatured == true)
.toList());
但是直接使用它会失败,因为temps
尚未初始化,因此您需要使用以下方法对其进行初始化:
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
但是,不是循环,更简单的方法是使用expand
函数:
List<Product> temps = companies.expand((company) => company.products.where((product) => product.isFeatured)).toList();
print('===> ${temps.length}'); // <----- here