How I can read List from last item to the first Item - flutter

I need to flip list to be able to read that list from last item to first
ex : list[a,b,c,d]
I want to get that list as [d,c,b,a];
anyone have Idea how to do that ?

You can use reversed.
An example,
List<String> list = ["a", "b", "c"];
list = list.reversed.toList();
print(list); // Result [c, b, a]
Document of reversed
/**
* Returns an [Iterable] of the objects in this list in reverse order.
*/
Iterable<E> get reversed;

Related

Flutter how to add a List with keys and values to another (Master) list with keys and values where the keys "id" are same in the 2 lists

I have small issues combining 2 Lists:
Here is a snippet -
List booked = []; has 5 elements with 5 keys and values. This is the list of only booked rooms.
List combinedList = []; has 10 elements with 42 keys and values. The is the master list of all rooms.
Not all elements of combinedList are inside booked. But all elements of booked are inside combinedList. This is where it gets tricky.
I am trying to add the content of List booked = [] into List combinedList = [] so that I can use EndDate and checkOutTime from the List booked = [].
When I use the method below, the length of the List fetchRooms is 5 and not 10.
Any idea how I can achieve this results?
I tried this code:
if (booked.isNotEmpty) {
combinedList.forEach((e) {
booked.forEach((ele) {
if (e["roomNumber"] == ele["roomNumber"]) {
// if (e["id"] == ele["id"]) {
fetchRooms.add(getExtendedlist(
e["buildingEntranceUrl"].toString(),
e["roomUrl"].toString(),
e["description"].toString(),
e["address"].toString(),
e["Name"].toString(),
e["averageRating"].toString(),
e["roomNumber"].toString(),
e["endDateAndTime"].toString(),
e["startDateAndTime"].toString(),
e["Monthly"].toString(),
e["RegisteredDate"].toString(),
e["accessType"].toString(),
e["geopointlat"],
e["geopointlng"],
e["id"],
ele["EndDate"].toString(),
ele["checkOutTime"].toString(),
));
}
});
});
}
but it gives me only length of booked. I want to have length of combinedList with the fields ele["EndDate"].toString() and ele["checkOutTime"].toString() added where the ids are same and blank where they are not.

Convert Array of list [(value)] or list of list ((value)) to list or array Flutter

I am asking for help because I don't know how to solve this issue.
I am currently mapping on an Array of object with this code to output an array of string :
contexts = snapshot.data!.myContexts
.where(
(e) => e.organisation.name == organizationValue)
.map((e) => e.projects.map((e) => e.name)).toList();
The last .toList make the difference in the output value from getting an array of list [()] and a list of list (()).
I wanted to know How I could get only a list of string or an array of string.
Thank you in advance, Weac
Can you try creating the list this way ?
final organization = snapshot.data!.myContexts
.firstWhere(
(e) => e.organisation.name == organizationValue);
contexts = List<String>.generate(organization.projects.length,(index)=>organization.projects[index].name);
Assuming the organization is always unique and you can't have two organization with the same name.

Remove duplicate dart

there is a list:
#Koolkid3
#Peetea
#Peetea
#Ruptan
#bulunabu
#ptygma
#ptygma
Here's what to get:
#Koolkid3
#Ruptan
#bulunabu
If there are duplicates, then you need to remove them completely. Therefore, toSet() will not work here. How to do it?
You can convert List to Set to remove duplicates. If needed again convert it into list.
List<String> list = ["a", "v", "a"];
print(list.toSet().toList()); //[a, v]
Removing duplicates and itself
List<String> list = ["a", "v", "a"];
list.removeWhere(
(String s1) => list.where((s2) => s1 == s2).length > 1,
);
print(list); /// [v]
What about the following:
List<String> list = [
"#Koolkid3",
"#Peetea",
"#Peetea",
"#Ruptan",
"#bulunabu",
"#ptygma",
"#ptygma"
];
var occurrenceCount = Map();
list.forEach((x) => occurrenceCount[x] = !occurrenceCount.containsKey(x) ? (1) : (occurrenceCount[x] + 1));
list.retainWhere((element) => occurrenceCount[element] == 1);
print(list);
Use a Map to count occurrence of each item, then use retainWhere to only keep the items that occur once.

Create a new list of random items with a specific length in Flutter/Dart

I have a list of strings, when I click a button I want to generate a new list with a specific number of items from my original list of strings.
I'm able to generate a random list with no repeating items but the number of items that returns is random. For example I want a new list of 5 items, sometimes it returns with 2 items other times 4 etc.
var randomList = new List.generate(
5, (_) => originalList[Random().nextInt(originalList.length)]).toSet().toList();
I've found lots of info for returning 1 random item, but nothing about multiples.
EDIT:
Thank you everyone for the responses, I was able to fix my problem by using shuffle like so
var randomList = (originalList..shuffle()).take(5).toList();
Your method of getting random items out of the original list will result in duplicates, which are then eliminated when you call .toSet(), which is why you get lists of varying lengths.
Instead of calling .toSet() to prevent duplicates, we need to be sure that no duplicates are chosen in the first place.
One way of doing that would be to generate a list of numbers from 0 up to originalList.length - 1, then shuffle them, and then choose the first n numbers from that list, and map them to values from original list.
void main() {
List<String> originalList = ["hello", "what", "goodbye", "Test", "Apple", "Banana"];
List<int> indices = List<int>.generate(originalList.length, (i) => i);
indices.shuffle();
int newCount = 3;
List<String> randomList = indices.take(newCount).map((i) => originalList[i]).toList();
print(randomList);
}
This method is efficient, and has a worst case performance of O(n), assuming dart has an efficient shuffle() implementation (which I'm sure it does).
The problem here is the list generates duplicates nonetheless.
var randomList = new List.generate(5, (_) => originalList[Random().nextInt(originalList.length)])
The list could contain [dello, kello, hello, gello, hello]
(for example, assume items are taken from the original list)
Now performing .toSet() on this will remove the duplicate hello from the list, this is what causes the list size to be random. In this instance it's two duplicates, but later on it could have more.
A simple solution for this would be
var randomList = [];
List.generate(5, (_) {
var randomString = originalList[Random().nextInt(originalList.length)];
while (randomList.contains(randomString)) {
randomString = originalList[Random().nextInt(originalList.length)];
}
randomList.add(randomString);
});

Adding an item to multiple lists at once

Imagine you have two lists :
- list A
- list B
The list A contains multiple list B,
I want to be able to add an item to all list B that are contain in the list A in one time.
I already make some research about it and i couldn't find any solution for my problem .
Thank you.
Easiest way would be to iterate through the list and add the string you want to add.
List<List<String>> listA = [[], [], [], []]; // I'm assuming your list
// is something like this
listA.forEach((listB) {
listB.add('String to be added');
});