我有电影列表.这包含所有动画&非动画电影.要识别它的Animated是否有一个名为isAnimated的标志.
我只想展示动画电影.我编写的代码只过滤掉动画电影,但却出现了一些错误.
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",new Movie(movieName: "Hate Story",isAnimated: false,rating: 1.0),new Movie(movieName: "Minions",]; 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" ),); } }
解决方法
toList()缺少to materializer结果
_AnimatedMovies = AllMovies.where((i) => i.isAnimated).toList();