Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Filter list as per some condition

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

like image 564
Tushar Pol Avatar asked Mar 30 '18 17:03

Tushar Pol


2 Answers

toList() is missing to materializer the result

_AnimatedMovies = AllMovies.where((i) => i.isAnimated).toList();
like image 125
Günter Zöchbauer Avatar answered Oct 23 '22 15:10

Günter Zöchbauer


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;
  }
like image 20
Rahul Raj Avatar answered Oct 23 '22 15:10

Rahul Raj