Good evening, I need some help in flutter when it comes to mapping nested objects.
Here I am getting a "Salon" document from firebase and I am mapping the data to it, knowing that the Agents data are "firebase references" to the Agents collection ex: /agents/id [you can check the image below], so basically what I'm doing is getting the reference then calling a custom getData() that maps on its turn agents data to an Agent instance and returns it as an item of a list of Agents.
So what I want is mapping the future List<Future < < Agent > > that results to the List < Agent > agents
Here is what've done so far
class Salon {
final String id;
final String name;
final String email;
final String image;
final String website;
final String phoneNumber;
final List<Agent> agents;
Salon({
this.id,
this.name,
this.email,
this.image,
this.website,
this.phoneNumber,
this.agents,
});
factory Salon.fromMap(Map data) {
return Salon(
id: data['id'] ?? '',
name: data['name'] ?? '',
website: data['website'] ?? '',
email: data['email'] ?? '',
image: data['image'] ?? 'default.png',
phoneNumber: data['phoneNumber'] ?? '',
agents: (data['agents'] as List ?? []).map((item) async {
Document<Agent> agentRef = item;
Agent agent = await agentRef.getData();
return agent;
}).toList(),
);
}
}