0

我有 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);
}
4

1 回答 1

0

当文档发生更改时,Firestore 会通知客户端,但不会通知该文档中的哪些特定字段已更改。如果您的应用程序需要这些信息,您必须DocumentSnapshot自己在应用程序代码中比较以前的版本和新版本。

于 2021-08-25T14:44:10.420 回答