'How do I link an int in a list to a string in a list in dart?
So I have created a list with the names of apps inside it. I sorted it in alphabetical order, printing the result. These apps have won the app of the year award for different years. How would I assign the year, an int, to a string in a list, for example, 2012 to FNB Banking, and print the result by calling for the year? So I've tried to put the years in a list but have no idea of how to link the two lists so backed out from that idea.
Solution 1:[1]
Did you try using a map<String,String>, you can set the map item key as the year and its value as the app.
it can be something like this:
List<String> apps = ["app_one","app_two"];
List<int> years = [2020,2021];
Map<int, String> appPerYear = {};
for (var i = 0; i < apps.length; i++) {
appPerYear[years[i]] = apps[i];
}
Then to print any app of a specific year use
print(appPerYear[2020]);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |