将 firestore 文档转换为 flutter 类

Ema*_*per 6 firebase flutter google-cloud-firestore

我有一个名为“ Consultant我的应用程序从用户那里收集数据”的类。

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,
  });
}
Run Code Online (Sandbox Code Playgroud)

下面是我的一位用户的示例:

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
];


Run Code Online (Sandbox Code Playgroud)

好的,所有这些用户信息将通过以下方式检索到正确的小部件中: consultant.[index].consultantDescription... etc.. 目前所有信息都已进入我的班级,但我需要移动到云 Firestore,在那里我收集用户的所有信息,以便我将在我的班级中使用一些东西使用 cloud firestore future 将信息动态添加到我的小部件中,例如:

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...
);
Run Code Online (Sandbox Code Playgroud)

这将允许我从数据库中获取信息并推送到正确的小部件,这可能吗?

And*_*zo1 6

要在本地类中转换来自 firestore 的数据,您可以在类中创建一个构造函数方法:

Consultant.fromSnapshot(Map<String, dynamic> snapshot)
      : consultantFirstName = snapshot['name'],
        consultantLastName = snapshot['last_name'],
        consultantNickName = snapshot['nickname'];
Run Code Online (Sandbox Code Playgroud)

第一列是您的本地类字段。右侧的“name”、“last_name”等是cloud firestore内的字段名称。然后你打电话:

DocumentSnapshot snapshot= await db.collection('consultants').doc(/*consultant id*/).get();
if(snapshot.exists) Consultant consultant = Consultant.fromSnapshot(snapshot.data());
Run Code Online (Sandbox Code Playgroud)

如果您还需要保存 ID,请执行以下操作:

Consultant.fromSnapshot(String id, Map<String, dynamic> snapshot)
      : consultantId= id,
        consultantFirstName = snapshot['name'],
        consultantLastName = snapshot['last_name'],
        consultantNickName = snapshot['nickname'];
Run Code Online (Sandbox Code Playgroud)

进而:

Consultant consultant = Consultant.fromSnapshot(snapshot.id,snapshot.data());
Run Code Online (Sandbox Code Playgroud)

对于构建小部件,最直接的方法是使用 FutureBuilder,它在检索信息时更新您的顾问集合