【问题标题】:Listen to changes and get info on updated fields only仅收听更改并获取有关更新字段的信息
【发布时间】: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);
}

【问题讨论】:

标签: firebase flutter dart google-cloud-firestore flutter-streambuilder


【解决方案1】:

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

【讨论】:

  • 谢谢,这是我需要知道的。
猜你喜欢
  • 1970-01-01
  • 2018-09-22
  • 2017-09-14
  • 1970-01-01
  • 2010-09-06
  • 1970-01-01
  • 2019-06-24
  • 2019-11-10
  • 1970-01-01
相关资源
最近更新 更多