I have a class called Consultant
where I collect data from the user for my app.
class Consultant {
final int id;
final String consultantFirstName;
final String consultantLastName;
final String consultantNickName;
final String consultantImageProfile;
final String consultantCategory;
final String consultantDescription;
final String consultantLikes;
final String consultantReviews;
final String consultantStars;
final String consultantPrice;
final String consultantExperience;
const Consultant({
this.id,
this.consultantFirstName,
this.consultantLastName,
this.consultantNickName,
this.consultantImageProfile,
this.consultantCategory,
this.consultantDescription,
this.consultantLikes,
this.consultantReviews,
this.consultantStars,
this.consultantPrice,
this.consultantExperience,
});
}
this below is an example of one of my user:
final Consultant marco = Consultant(
id: 1,
consultantFirstName: 'Marco',
consultantLastName: 'Marcello',
consultantNickName: 'Tarot and Dreams',
consultantCategory: 'Tarocchi',
consultantDescription: 'Ciao a tutti sono Rocco',
consultantImageProfile: 'assets/images/rocco.jpg',
consultantLikes: '2342',
consultantReviews: '76245',
consultantPrice: '3.90',
consultantExperience: '12',
);
List<Consultant> consultant = [
marco,
carmela,
sabrina,
saverio,
pamela,
giovanni
];
ok all this user information will be retrieved into the right widget by:
consultant.[index].consultantDescription... etc..
at the moment all the information are into my class but I need to move to cloud firestore where I collect all the information of the user so I would use into my class something to use cloud firestore future to have the information dynamically added to my widget for example:
final Consultant user = Consultant(
id: 1,
consultantFirstName: data[here the documents from firestore],
consultantLastName: data[here the documents from firestore],
consultantNickName: data[here the documents from firestore],
consultantCategory: data[here the documents from firestore],
consultantDescription: data[here the documents from firestore],
consultantImageProfile: data[here the documents from firestore],
etc...
);
this will allow me to grab the information from database and push to the right widget is that possible?
To convert data from firestore in a local class you can create a constructor method in your class:
Consultant.fromSnapshot(Map<String, dynamic> snapshot)
: consultantFirstName = snapshot['name'],
consultantLastName = snapshot['last_name'],
consultantNickName = snapshot['nickname'];
In the first column there are your local class fields. On the right 'name', 'last_name', etc. are the names of the fields inside cloud firestore. Then you call:
DocumentSnapshot snapshot= await db.collection('consultants').doc(/*consultant id*/).get();
if(snapshot.exists) Consultant consultant = Consultant.fromSnapshot(snapshot.data());
If you need to save also the ID do:
Consultant.fromSnapshot(String id, Map<String, dynamic> snapshot)
: consultantId= id,
consultantFirstName = snapshot['name'],
consultantLastName = snapshot['last_name'],
consultantNickName = snapshot['nickname'];
and then:
Consultant consultant = Consultant.fromSnapshot(snapshot.id,snapshot.data());
For building widgets, the most straightforward way is using a FutureBuilder that updates your collection of consultants when information is retrieved
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