如何从Dart中的类列表中检索属性?

问题描述 投票:-1回答:1

我在这里创建了一个包含4个元素的自定义类

class Top {
  String videoId;
  int rank;
  String title;
  String imageString;

  Top({this.videoId, this.rank, this.title, this.imageString});
}

我正在检索一些Firebase项目来填充这些元素。

var top = new Top(videoId: items['vidId'], rank: items['Value'],
title: items['vidTitle'], imageString: items['vidImage']);

然后我将它们添加到Type“Top”列表中,以便根据“rank”对Class值进行排序...

List<Top> videos = new List();
videos..sort((a, b) => a.rank.compareTo(b.rank));
videos.add(top);

但是,打印videos记录这个......

[Instance of 'Top', Instance of 'Top', Instance of 'Top', Instance of 'Top', Instance of 'Top', Instance of 'Top', Instance of 'Top', Instance of 'Top']

我不确定它是否因为它在列表中。如何从视频中获取可用的“热门”属性值?例如,当我查询top.rank时,我得到了这个......

[14, 12, 11, 10, 10, 6, 5, 1]
list class dart element flutter
1个回答
4
投票

使用[]运算符传递元素的索引来获取列表的属性。

如果你想在列表qazxsw poi中检索第三个qazxsw poi,你可以像访问它一样

Top

如果你想在videos列表中找到第三个qazxsw poi的属性qazxsw poi,你可以像访问它一样访问它

videos[3]

如果您希望print语句显示列表项,则更改您的类以覆盖rank方法,如

Top

希望有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.