gpt4 book ai didi

Flutter:根据某些条件过滤列表

转载 作者:IT老高 更新时间:2023-10-28 13:48:59 24 4
gpt4 key购买 nike

我有一个电影列表。这包含所有动画和非动画电影。为了确定它是否是动画,有一个名为 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",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

最佳答案

toList() 缺少实现结果

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

关于Flutter:根据某些条件过滤列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49578529/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com