我想要更新某个特定文档的字段,同时监听其值的任何更改。因此,基本上,当字段'call‘被发现为1时,我们将在打印消息后再次将其更新回0。然而,更新需要花费太多的时间。另一方面,使用python更新同一个字段要快得多。请告诉我该怎么做。
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));发布于 2022-04-25 05:51:53
文档编写所需时间与网络连接速度有关。但是你可以通过压缩数据来加快数据上传的速度。Dart有一个有用的工具,如GZip。GZip可以压缩高达99%的String数据,您可以以多种方式压缩decode/encode数据,甚至HttpClient也可以对其进行autoUncompress :)。
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);
}https://stackoverflow.com/questions/71990666
复制相似问题