190

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

I want to show only Animated movies. How can I do that?

4 Answers 4

380

toList() is missing to materializer the result

_AnimatedMovies = AllMovies.where((i) => i.isAnimated).toList();
5
  • 2
    No work here :( . It says type 'Where Iterable<>' is not a subtype of type 'List Aug 20, 2018 at 11:34
  • 5
    If you need a List add .toList() Aug 20, 2018 at 11:40
  • 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; Oct 23, 2021 at 11:31
  • What would be the null safe version of this? Oct 6, 2022 at 9:50
  • @JoelGMathew I think AllMovies.whereNotNull().where((i) => i.isAnimated).toList(); (not tested) Nov 1, 2022 at 15:13
26

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;
  }
9

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;
});
3

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);

Edited:

As per Günter Zöchbauer solution, we can use a single line instead of multiple. So the code is

_AnimatedMovies = AllMovies.where((i) => i.isAnimated).toList();
2
  • 2
    Iterables have a toList() method which is much easier to read IMHO. Mar 14, 2020 at 17:23
  • Agreed @GünterZöchbauer. We can use AllMovies.where((i) => i.isAnimated).toList(); as you mentioned Sep 20, 2022 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.