Flutter: Filter list as per some condition

160,331

Solution 1

toList() is missing to materializer the result

_AnimatedMovies = AllMovies.where((i) => i.isAnimated).toList();

Solution 2

The Solution is here

Just try with this Function getCategoryList(),
Here the condition will be catogory_id == '1' from the list

List<dynamic> getCategoryList(List<dynamic> inputlist) {
    List outputList = inputlist.where((o) => o['category_id'] == '1').toList();
    return outputList;
  }

Solution 3

You can use toList() method to get your desire output like following

toList() Collects all elements of this stream in a List.

To fix the above issue:

Add a toList() (This code creates a List<dynamic>)

_AnimatedMovies = AllMovies.where((i) => i.isAnimated).toList();

instead of

_AnimatedMovies = AllMovies.where((i) => i.isAnimated);

The output of where() is another Iterable, and you can use it as such to iterate over it or apply other Iterable methods. In the next example, the output of where() is used directly inside the for-in loop.

var evenNumbers = AllMovies.where((i) => i.isAnimated).toList();
for (var i in evenNumbers) {
  print('$i');
}
  1. You can also Use takeWhile

The methods takeWhile() can also help you filter elements from an Iterable.

 _AnimatedMovies = AllMovies.takeWhile((i) => i.isAnimated).toList();

enter image description here

Solution 4

You can use this for specific condition

List<String> strings = ['one', 'two', 'three', 'four', 'five'];
List<String> filteredStrings  = strings.where((item) {
   return item.length == 3;
});

Solution 5

where function on a List returns Iterable. You have to convert it to List using the function List.from(Iterable).

So in the above scenario you should use the following code snippet.

Iterable _AnimatedMoviesIterable = AllMovies.where((i) => i.isAnimated);

_AnimatedMovies = List.from(_AnimatedMoviesIterable);

Share:
160,331
Tushar Pol
Author by

Tushar Pol

My interest lies in AngularJS, Angular, Flutter.

Updated on September 22, 2021

Comments

  • Tushar Pol
    Tushar Pol over 2 years

    I'm having a list of Movies. That contains all Animated & non Animated Movies. To identify whether it's Animated or not there is one flag called isAnimated.

    I want to show only Animated movies. I have written code to filter out only Animated movies but getting some error.

    import 'package:flutter/material.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'Flutter Demo',
          theme: new ThemeData(
    
            primarySwatch: Colors.blue,
          ),
          home: new HomePage(),
        );
      }
    }
    
    class Movie {
      Movie({this.movieName, this.isAnimated, this.rating});
      final String movieName;
      final bool isAnimated;
      final double rating;
    }
    
    List<Movie> AllMovies = [
      new Movie(movieName: "Toy Story",isAnimated: true,rating: 4.0),
      new Movie(movieName: "How to Train Your Dragon",isAnimated: true,rating: 4.0),
      new Movie(movieName: "Hate Story",isAnimated: false,rating: 1.0),
      new Movie(movieName: "Minions",isAnimated: true,rating: 4.0),
    ];
    
    
    
    class HomePage extends StatefulWidget{
      @override
      _homePageState createState() => new _homePageState();
    }
    
    
    class _homePageState extends State<HomePage> {
    
      List<Movie> _AnimatedMovies = null;
    
      @override
      void initState() {
        super.initState();
        _AnimatedMovies = AllMovies.where((i) => i.isAnimated);
      }
    
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: new Container(
            child: new Text(
                "All Animated Movies here"
            ),
          ),
        );
      }
    }
    

    WhereIterable is not subtype of type List

  • Saeesh Tendulkar
    Saeesh Tendulkar over 5 years
    No work here :( . It says type 'Where Iterable<>' is not a subtype of type 'List
  • Günter Zöchbauer
    Günter Zöchbauer over 5 years
    If you need a List add .toList()
  • Günter Zöchbauer
    Günter Zöchbauer about 4 years
    Iterables have a toList() method which is much easier to read IMHO.
  • Shoaib Khan
    Shoaib Khan over 2 years
    It worked like charm actually I was creating an app in which I have some static data (list of a model) and when I am navigating to a single view page I am sending the id of the model and I am finding the model by id and showing it's data in the single view page. I am doing like this CompanyModel company = companies.where((c) => c.id == companyId).toList().first;