gpt4 book ai didi

firebase - 将 firestore 文档转换为 flutter 类

转载 作者:行者123 更新时间:2023-12-04 11:23:16 27 4
gpt4 key购买 nike

我有一个类(class)叫 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,
});
}
以下是我的一位用户的示例:
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
];


好的,所有这些用户信息都将通过以下方式检索到正确的小部件中: consultant.[index].consultantDescription... etc..目前所有信息都在我的类(class)中,但我需要转移到 cloud firestore,在那里我收集用户的所有信息,所以我会在我的类(class)中使用一些东西来使用 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...
);
这将允许我从数据库中获取信息并推送到正确的小部件
那可能吗?

最佳答案

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

Consultant.fromSnapshot(Map<String, dynamic> snapshot)
: consultantFirstName = snapshot['name'],
consultantLastName = snapshot['last_name'],
consultantNickName = snapshot['nickname'];
在第一列中有您的本地类字段。右侧的“name”、“last_name”等是 cloud firestore 中字段的名称。然后你打电话:
DocumentSnapshot snapshot= await db.collection('consultants').doc(/*consultant id*/).get();
if(snapshot.exists) Consultant consultant = Consultant.fromSnapshot(snapshot.data());
如果您还需要保存 ID,请执行以下操作:
Consultant.fromSnapshot(String id, Map<String, dynamic> snapshot)
: consultantId= id,
consultantFirstName = snapshot['name'],
consultantLastName = snapshot['last_name'],
consultantNickName = snapshot['nickname'];
进而:
Consultant consultant = Consultant.fromSnapshot(snapshot.id,snapshot.data());
对于构建小部件,最直接的方法是使用 FutureBuilder 在检索信息时更新您的顾问集合

关于firebase - 将 firestore 文档转换为 flutter 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65073533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com