简体   繁体   English

Flutter:更新单个文档时,StreamBuilder 获取其他 Firestore 文档

[英]Flutter: StreamBuilder gets other firestore documents when updating a single document

I have a Stream like this:我有一个像这样的 Stream:

@override
  void initState() {
    super.initState();
    rentsStream = FirebaseProfile().getRents(widget.portfolio.id);
  }

Widget _rentsStream() {
    return StreamBuilder<QuerySnapshot>(
      stream: rentsStream,
      builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
        if (snapshot.hasError) return Text('Error: ${snapshot.error}');
        if (!snapshot.hasData) return const Loader();
        if (snapshot.data == null) return const Loader();

        rents = snapshot.data!.docs;
        return _portfolioBuilder(context);
      },
    );
  }

The stream is declared outside in order to not rebuild the stream every time I change the state.每次更改 state 时,都会在外部声明 stream,以免重建 stream。

Here I get a list of rents.在这里,我得到了一份租金清单。 When I modify one of them, this one gets updated but the problem is that it comes with the rest of rents incrementing the documents reads monthly quota (and also I don't need to get again the non updated ones).当我修改其中一个时,这个会更新,但问题是它附带了租金的 rest 增加了文件读取每月配额(而且我不需要再次获得未更新的)。

Is possible to just get the one I updated?有可能只得到我更新的那个吗? Otherwise I'd need to save them locally and update the rents based on what I changed.否则我需要将它们保存在本地并根据我的更改更新租金。

What platform are you running this on?你在哪个平台上运行这个? On native mobile, the Firestore caches documents by default and (while your code will get called with all documents) only the modified ones are actually read from the server.在本机移动设备上,Firestore 默认缓存文档,并且(虽然您的代码将与所有文档一起调用)只有修改过的文档才会实际从服务器读取。

This local persistence caching is also available on Web, but there you have to explicitly enable it.这种本地持久性缓存在 Web 上也可用,但您必须显式启用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM