作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下UML类图:
UML class diagram
我想显示每个公司中所有特色产品的列表 View 。
我希望只获得特色产品:
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.
);
},
),
),
);
}
}
最佳答案
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
关于flutter - 如何计算并获得 Dart 和 Dart 中指定值的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63874707/
在我的应用程序中,我想检测手指按在屏幕上的类型。触摸类型可能是单点触摸或多点触摸。我搜索了很多,但找不到任何东西。 有什么方法可以找到类型吗? 最佳答案 不可能知道哪个手指在某个点,但您肯定可以获得每
以下代码是我一直在尝试用于多点触控的代码。手指一设置正确并在我拖动手指时四处移动。当我触摸并松开手指时,第二个手指出现并消失,但它永远不会移动。知道有什么问题吗? 我已阅读 developers bl
我是一名优秀的程序员,十分优秀!