I have this list of maps.
[
{title: 'Avengers', release_date: '10/01/2019'},
{title: 'Creed', release_date: '10/01/2019'}
{title: 'Jumanji', release_date: '30/10/2019'},
]
I would like to write a code that would group the list of movies by release_date like that.
[
{
"10/01/2019": [
{
"title": "Avengers"
},
{
"title": "Creed"
}
]
},
{
"30/10/2019": [
{
"title": "Jumanji"
}
]
}
]
Required Properties of the Grouped ListView: elements: It contains a list of data, which we have to display in the list. These properties are very important. groupBy: A function using which to map content and groups. itemBuilder: This functions as the widget that defines the content of the app.
fromIterable() to convert the list to a map. In the method for the first argument, we pass myList as an iterable. The method then calculates the key and value for each element of the iterable.
The package collection implements
the groupBy function.
For grouping by date:
import "package:collection/collection.dart";
main(List<String> args) {
var data = [
{"title": 'Avengers', "release_date": '10/01/2019'},
{"title": 'Creed', "release_date": '10/01/2019'},
{"title": 'Jumanji', "release_date": '30/10/2019'},
];
var newMap = groupBy(data, (Map obj) => obj['release_date']);
print(newMap);
}
For removing the release_date key from each map entry:
var newMap = groupBy(data, (Map obj) => obj['release_date']).map(
(k, v) => MapEntry(k, v.map((item) { item.remove('release_date'); return item;}).toList()));
For changing a key:
var newMap = groupBy(data, (Map obj) => obj['release_date']).map(
(k, v) => MapEntry(k, v.map((item) => {'name': item['title']}).toList()));
If you have Dart 2.7, you can extend Iterable to add a useful groupBy method:
extension Iterables<E> on Iterable<E> {
Map<K, List<E>> groupBy<K>(K Function(E) keyFunction) => fold(
<K, List<E>>{},
(Map<K, List<E>> map, E element) =>
map..putIfAbsent(keyFunction(element), () => <E>[]).add(element));
}
Now, you're List of Maps, could be grouped using something like:
final releaseDateMap = listOfMaps.groupBy((m) => m['release_date'])
Data like this:
[
{title: 'Avengers', release_date: '10/01/2019'},
{title: 'Creed', release_date: '10/01/2019'}
{title: 'Jumanji', release_date: '30/10/2019'},
]
would turn into:
{
'10/01/2019': [
{title: 'Avengers', release_date: '10/01/2019'},
{title: 'Creed', release_date: '10/01/2019'}
],
'30/10/2019': [
{title: 'Jumanji', release_date: '30/10/2019'},
]
}
extension UtilListExtension on List{
groupBy(String key) {
try {
List<Map<String, dynamic>> result = [];
List<String> keys = [];
this.forEach((f) => keys.add(f[key]));
[...keys.toSet()].forEach((k) {
List data = [...this.where((e) => e[key] == k)];
result.add({k: data});
});
return result;
} catch (e, s) {
printCatchNReport(e, s);
return this;
}
}
}
then use it like this
var data = [
{"title": 'Avengers', "release_date": '10/01/2019'},
{"title": 'Creed', "release_date": '10/01/2019'},
{"title": 'Jumanji', "release_date": '30/10/2019'},
];
var result = data.groupBy('title');
print(result);
then the result is
[{10/01/2019: [{title: Avengers, release_date: 10/01/2019}, {title: Creed, release_date: 10/01/2019}]}, {30/10/2019: [{title: Jumanji, release_date: 30/10/2019}]}]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With