gpt4 book ai didi

flutter - 如何计算并获得 Dart 和 Dart 中指定值的列表

转载 作者:行者123 更新时间:2023-12-03 03:49:56 25 4
gpt4 key购买 nike

我有以下UML类图:
UML class diagram
我想显示每个公司中所有特色产品的列表 View 。
我希望只获得特色产品:

  • 产品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
    这是带有数据的 模型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/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com