【发布时间】:2021-10-25 08:00:14
【问题描述】:
我有 Flutter/Firebase 应用程序,它允许用户将他们的图书阅读数据存储到 Firestore(已阅读的图书、当前正在阅读的图书等)。我想实现一个功能,允许用户查看是否有人完成阅读一本书(FS 卷对象字段“completedUsers”更新)或有人开始阅读新书(FS 帐户对象字段“nowReading”更新)。
我认为我应该使用 CollectionReference().snapshots().listen() - 方法,但我还没有弄清楚如何以及如何使用 StreamBuilder 进行设置,所以我可以获得关于哪个的确切信息部分 db 对象已更新。
这是我的用户帐户和数量模型:
@JsonSerializable(explicitToJson: true)
class Account {
String name;
String uid;
List<Volume> nowReading;
List<Volume> wantToRead;
List<Account> friends;
Map<String, Volume> tips;
Account(
{this.name,
this.uid,
this.nowReading,
this.wantToRead,
this.friends,
this.tips});
factory Account.fromJson(Map<String, dynamic> json) =>
_$AccountFromJson(json);
Map<String, dynamic> toJson() => _$AccountToJson(this);
}
@JsonSerializable(explicitToJson: true)
class Volume {
String id;
String title;
String description;
String smallThumbnail;
String bigThumbnail;
List<String> authors;
List<String> categories;
int published;
int pageCount;
double averageGoogleRating;
double averageUserRating;
List<UserReview> userReviews;
List<String> completedUsers;
Volume(
{this.id,
this.title,
this.description,
this.smallThumbnail,
this.bigThumbnail,
this.authors,
this.categories,
this.published,
this.pageCount,
this.averageGoogleRating,
this.averageUserRating,
this.userReviews,
this.completedUsers});
factory Volume.fromJson(Map<String, dynamic> json) => _$VolumeFromJson(json);
Map<String, dynamic> toJson() => _$VolumeToJson(this);
}
【问题讨论】:
-
您是否查看过以下 Flutterfire 文档部分:firebase.flutter.dev/docs/firestore/usage/#realtime-changes?
-
啊,是的,我有。我可以获取更新了哪本书/卷的信息,但问题是我怎么知道是哪个字段?
标签: firebase flutter dart google-cloud-firestore flutter-streambuilder