首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firestore花费了太多时间从flutter应用程序中更新

Firestore花费了太多时间从flutter应用程序中更新
EN

Stack Overflow用户
提问于 2022-04-24 17:17:46
回答 1查看 149关注 0票数 0

我想要更新某个特定文档的字段,同时监听其值的任何更改。因此,基本上,当字段'call‘被发现为1时,我们将在打印消息后再次将其更新回0。然而,更新需要花费太多的时间。另一方面,使用python更新同一个字段要快得多。请告诉我该怎么做。

代码语言:javascript
复制
 FirebaseFirestore.instance
        .collection('joystick')
        .snapshots()
        .listen((QuerySnapshot querySnapshot) {

      var  firestoreList = querySnapshot.docs;
      var data = firestoreList.first.data();

      var callval = firestoreList.first.get('call');
      print("call value while listening ${callval}");

      if(callval == 1){
        print("call value is 1 ");

        //makecall();
        FirebaseFirestore.instance.collection('joystick').doc('data').update({'call':0});
        call = firestoreList.first.get('call');
        print("Next call value is ${call}");

        if(call == 0){
          print("updated!");
          callval = 0;
        }
      }
   }).onError((e) => print(e));
EN

回答 1

Stack Overflow用户

发布于 2022-04-25 05:51:53

文档编写所需时间与网络连接速度有关。但是你可以通过压缩数据来加快数据上传的速度。Dart有一个有用的工具,如GZipGZip可以压缩高达99%的String数据,您可以以多种方式压缩decode/encode数据,甚至HttpClient也可以对其进行autoUncompress :)。

代码语言:javascript
复制
import 'dart:io';

// Send compressed data.
Future<void> sendCompressed() async {
  final gzip = GZipCodec();
  // You can convert any data to JSON string with `dart:convert`.
  const String jsonToSend = '{"field": "Some JSON to send to the server."}';
  // Original Data.
  final List<int> original = utf8.encode(jsonToSend);
  // Compress data.
  final List<int> compressed = gzip.encode(original);
  // Send compressed to db.
  print(compressed);
}

// Get compressed data.
Future<void> getCompressed() async {
  final gzip = GZipCodec();
  // Get compressed data from the data base.
  final List<int> compressed = await http.get('https://data.com');
  // Decompress
  final List<int> decompress = gzip.decode(compressed);
  // Decode back to String (JSON)
  final String decoded = utf8.decode(decompress);
  // Do what you want with decoded data.
  print(decoded);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71990666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档