在Flutter / Dart中映射多个列表?

时间:2017-10-19 22:48:56

标签: list sorting dart element flutter

我有SWIFT代码,我已经映射了一组具有相同数量元素的列表,其中1是一个int列表,它们按降序映射。这是我的SWIFT代码......

class Video
{
    let videoID: String
    let rank: Int
    let title: String
    let imageString: String

    init(videoID: String, rank: Int, title: String, imageString: String)
    {
        self.videoID = videoID
        self.rank = rank
        self.title = title
        self.imageString = imageString
    }
}

var videos = [Video]()

videos.sort { $0.rank < $1.rank }

self.vidTitleArray = videos.map { $0.title }
self.vidRankArray = videos.map { $0.rank }
self.vidIdArray = videos.map { $0.videoID }
self.vidImageArray = videos.map { $0.imageString }

这就是我在Flutter的地方,但我在前进中遇到错误......

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

  videos(this.videoId, this.rank, this.title, this.imageString);
}

如何从类构造函数中创建list,然后按照降序映射到排名重新排列所有元素?

*****选项*****

这是我的代码,不使用Video类。我使用SplayTreeMap正确排名,但如果Value始终相同,则只返回1个节点。有没有办法映射以Value降序的所有4张地图?

映射myMap = event.data.snapshot.value; //存储每张地图

                          var titles = myMap.values;

                          List onesTitles = new List();
                          List onesIds = new List();
                          List onesImages = new List();
                          List onesRank = new List();

                          for (var items in titles) {
                            onesTitles.add(items['vidTitle']);
                            onesIds.add(items['vidId']);
                            onesImages.add(items['vidImage']);
                            onesRank.add(items['Value']);
                          }

                          names = onesTitles;
                          ids = onesIds;
                          numbers = onesRank;
                          vidImages = onesImages;

1 个答案:

答案 0 :(得分:3)

您可以使用List.sort()来实现此目的,我创建了一个自定义示例,其中我对Videos类的3个实例进行了硬编码,然后在List的构建方法中添加了它们按升序排列。使用List.sort()compareTo,我可以按降序重新排列它们。

List<Videos> myList = [_firstVideos, _secondVideos, _thirdVideos];
sort() {
      for (var video in myList) {
        print(video.rank); ///prints 1 2 3 
      }
      myList.sort((y, x) => x.rank.compareTo(y.rank));

      for (var video in myList) {
        print(video.rank);  ///prints 3 2 1
      }
    }

...

import 'package:flutter/material.dart';


    void main(){
      runApp(new MaterialApp(home: new MyApp(),));
    }

    class MyApp extends StatelessWidget {

      Videos _firstVideos = new Videos(
        videoId: "0001",
        rank: 1,
        title: "My First Video",
        imageString: "My First Image",
      );
      Videos _secondVideos = new Videos(
        videoId: "0002",
        rank: 2,
        title: "My Seocnd Video",
        imageString: "My Second Image",
      );

      Videos _thirdVideos = new Videos(
        videoId: "0003",
        rank: 3,
        title: "My Third Video",
        imageString: "My Third Image",
      );

      @override
      Widget build(BuildContext context) {
        List<Videos> myList = [_firstVideos,_secondVideos,_thirdVideos];
        sort()
        {
          myList.sort((y,x)=>x.rank.compareTo(y.rank));

        print(myList[0].title);
        }
        return new Scaffold(
          body: new Center(
            child: new FlatButton(onPressed: sort, child: new Text("Sort")),
          ),
        );
      }
    }


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

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