Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert firestore document into a flutter class

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?

like image 691
Emanuel Developer Avatar asked Nov 30 '20 12:11

Emanuel Developer


1 Answers

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

like image 114
AndreaCostanzo1 Avatar answered Sep 30 '22 09:09

AndreaCostanzo1