Mapping Multiple Lists in Flutter/Dart?
You can use List.sort()
for that purpose, I have created a custom example where I hard coded 3 instances of your Videos
class, and then added them in my build method in a List
in ascending order. Using List.sort()
and compareTo
, I was able to rearrange them in an descending order.
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});
}
Charles Jr
Jack of All Trades - Master of Few!! Started with Obj-C.then(Swift)!.now(Flutter/Dart)! Thank everyone for every answer, comment, edit, suggestion. Can't believe I've been able to answer a few myself :-)
Updated on December 03, 2022Comments
-
Charles Jr over 1 year
I have SWIFT code where I've mapped a set of lists with equal number of elements where 1 was a list of ints and they were mapped in descending order. Here is my SWIFT Code...
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 }
Here is where I am in Flutter, but I'm getting errors going forward...
class videos{ String videoId; int rank; String title; String imageString; videos(this.videoId, this.rank, this.title, this.imageString); }
How do I make a
list
from the class constructor then rearrange all elements in descending order mapped to rank?***** OPTION *****
Here is my code without using a
Video
class. I got it to rank correctly using aSplayTreeMap
but ifValue
is ever the same it only brings back 1 node. Is there a way to map all 4 maps descending withValue
?Map myMap = event.data.snapshot.value; //store each map
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;
-
Shady Aziza over 6 yearsI have edited the answer to make it more clear on how to use List.sort().
-
-
Charles Jr over 6 years@azia Please look at my other
OPTION
I've added. I'm trying to sort without buildingVideo
lists