title: How to count and get a list of specified value in dart and flutter
body: <p>I have the following UML class diagram:
  <a href="https://i.stack.imgur.com/yJKvh.png" rel="nofollow noreferrer">UML class diagram</a></p>
  <p>I want to display a list view of all featured products found in each company.
  I expect to get only <strong>featured products</strong>:</p>
  <ul>
  <li>product 00 (from company abc)</li>
  <li>product 02 (from company abc)</li>
  <li>product 11 (from company xyz)</li>
  </ul>
  <p><em>but the below code give me only the first item <strong>product 00</strong>.</em></p>
  <p>I tried to counts the <strong>featured products</strong>
  but the length always 1.</p>
  <pre><code>List&lt;Product&gt; temps;
      for (var i = 0; i &lt; companies.length; i++) {
        
        temps = companies[i]
            .products
            .where((fp) =&gt; fp.isFeatured == true)
            .toList();
      }
      print('===&gt; ${temps.length}'); // &lt;----- here
  </code></pre>
  <p>This is the model <strong>Dart</strong> code with the data:</p>
  <pre><code>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&lt;Product&gt; products;
  
    Company({
      this.id,
      this.name,
      this.products,
    });
  }
  
  List&lt;Product&gt; 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&lt;Product&gt; productsOfCompanyXyz = [
    Product(
      id: 0,
      name: 'product 11',
      price: 30.0,
      isFeatured: true,
    ),
    Product(
      id: 1,
      name: 'product 12',
      price: 40.0,
      isFeatured: false,
    ),
  ];
  
  List&lt;Company&gt; companies = [
    Company(
      id: 0,
      name: 'Company abc',
      products: productsOfCompanyAbc,
    ),
    Company(
      id: 1,
      name: 'Company xyz',
      products: productsOfCompanyXyz,
    )
  ];
  </code></pre>
  <p>This is the <strong>Flutter</strong> sample:</p>
  <pre><code>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() =&gt; _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
  
    int countFeaturedProducts() {
      List&lt;Product&gt; temps;
      for (var i = 0; i &lt; companies.length; i++) {
        
        temps = companies[i]
            .products
            .where((fp) =&gt; fp.isFeatured == true)
            .toList();
        // this product TRUE items that is featured
        for (var item in temps) {
          print(item.name);
         
        }
      }
      print('===&gt; ${temps.length}');
      print('===&gt; out');
      return temps.length;
    }
  
  
  List&lt;Product&gt; featuredProducts(int index){
      List&lt;Product&gt; allFeatured;
      for (var i = 0; i &lt; 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(), //&lt;------- Here give length 1.
            itemBuilder: (context, index) {
              return ListTile(
                title: Text(featuredProducts(index)[index].name), //&lt;------- Here give only one Product 00.
              );
            },
          ),
        ),
      );
    }
  }
  </code></pre>