本文介绍了Flutter:根据某些条件过滤列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有电影列表。其中包含所有Animated&非动画电影。要确定是否有动画,是否有一个标记叫做 isAnimated

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

我只想显示动画电影。我编写了代码,只过滤了动画电影,但出现了一些错误。

I want to show only Animated movies. I 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"
        ),
      ),
    );
  }
}

推荐答案

toList()缺少实现的结果

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

这篇关于Flutter:根据某些条件过滤列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:30