web-dev-qa-db-fra.com

Flutter: liste de filtres selon certaines conditions

Je vais avoir une liste de films. Qui contient tous les films animés et non animés. Pour identifier s'il est animé ou non, il existe un indicateur appelé isAnimated .

Je veux montrer uniquement des films d'animation. J'ai écrit du code pour filtrer uniquement les films d'animation, mais obtenir une erreur.

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<Object> is not subtype of type List<Object>

24
Tushar Pol

toList() manque pour matérialiser le résultat

_AnimatedMovies = AllMovies.where((i) => i.isAnimated).toList();
60
Günter Zöchbauer