class People {
final String id;
final String title;
final String name;
final String location;
final String email;
final String lga;
People({
@required this.id,
@required this.title,
@required this.name,
@required this.location,
this.email,
this.lga,
});
factory People.fromJson(Map<String, dynamic> json) => People(
id: json["id"],
title: json["title"],
name: json["name"],
location: json["location"],
email: json["email"],
lga: json["lga"],
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"name": name,
"location": location,
"email": email,
"lga": lga,
};
@override
bool operator ==(o) =>
o is Priest && townOrigin == o.townOrigin && lga == o.lga;
@override
int get hashCode => town.hashcode ^ lga.hashcode;
This is possible in a listView between town or lga but my problem is, I want to implement these equals and hashCode
in locations too, differently in another ListView.
To make it simple, many people have the same location but I only need one location, from many (as a header) of people from the same location and iterate their names below.
Any help?
[
{
"id": "1",
"name": "Ada Kenneth Kenechukwu",
"location": "Awka",
"townOrigin": "Nimo",
"phone": "080633",
"lga": "Njikoka",
},
{
"id": "2",
"name": "Ugo John",
"location": "Awka",
"townOrigin": "Nimo",
"phone": "08057763",
"lga": "Njikoka",
},
{
"id": "3",
"name": "Okafor Kenneth Kenechukwu",
"location": "Awka",
"townOrigin": "Ukpo",
"phone": "0806363",
"lga": "Dunukofia",
},
{
"id": "4",
"name": "Ndu Agi",
"location": "Awka",
"townOrigin": "Ukpo",
"phone": "0806763",
"lga": "Dunukofia",
},
{
"id": "5",
"name": "Mma Peter",
"location": "Awka",
"townOrigin": "Ichida",
"phone": "08057763",
"lga": "Anaocha",
},
{
"id": "6",
"name": "Eze Peter",
"location": "Awka",
"townOrigin": "Ichida",
"phone": "0806345",
"lga": "Anaocha",
}
]
When you look at this, some have the same location, townOrigin,lga. I have three listView pages in my app where it will display all those(names) from the same location,townOrigin,lga alone respectively . Displaying only names but using one element(like Awka in location) as a subhead.The same applies to all townOrigin, and lga. Looking forward for help.
I have gotten something better, but yet to solve all my issues, with this package sticky_grouped_list, https://pub.dev/packages/sticky_grouped_list
I wanted a group inside a group. I have gotten a group of lga but yet in location inside lga group.
How to merge people from the same location like "Uga" into one.
You could try adding something like this:
extension GroupingBy on Iterable<dynamic> {
Map<String, List<dynamic>> groupingBy(String key){
var result = <String, List<dynamic>>{};
for(var element in this){
result[element[key]] = (result[element[key]] ?? [])..add(element);
}
return result;
}
}
This will let you group your data elements by a particular field.
First, put your data into a list:
var people = [
{
"id": "1",
"name": "Ada Kenneth Kenechukwu",
"location": "Awka",
"townOrigin": "Nimo",
"phone": "080633",
"lga": "Njikoka",
},
{
"id": "2",
"name": "Ugo John",
"location": "Awka",
"townOrigin": "Nimo",
"phone": "08057763",
"lga": "Njikoka",
},
{
"id": "3",
"name": "Okafor Kenneth Kenechukwu",
"location": "Awka",
"townOrigin": "Ukpo",
"phone": "0806363",
"lga": "Dunukofia",
},
{
"id": "4",
"name": "Ndu Agi",
"location": "Awka",
"townOrigin": "Ukpo",
"phone": "0806763",
"lga": "Dunukofia",
},
{
"id": "5",
"name": "Mma Peter",
"location": "Awka",
"townOrigin": "Ichida",
"phone": "08057763",
"lga": "Anaocha",
},
{
"id": "6",
"name": "Eze Peter",
"location": "Awka",
"townOrigin": "Ichida",
"phone": "0806345",
"lga": "Anaocha",
},
{
"id": "7",
"name": "New Person",
"location": "Somewhere else",
"townOrigin": "idk",
"phone": "694201738",
"lga": "Mystery",
}
];
Now, if you want to group them by location, you can do that simply, like so:
var peopleByLocation = people.groupingBy('location');
var awka = peopleByLocation['Awka']; //List of all people with a 'location' : 'Awka'
var somewhereElse = peopleByLocation['Somewhere else'];
Here's a link to a slightly modified version in dartpad that you can play around with: https://dartpad.dev/8a53fc2eb4f63d904f414ea528574baf