我正在为我的应用程序使用https://github.com/boeledi/blocs中描述的 bloc 模式。在应用程序中,我基本上是在 Firestore 中获取和写入数据。最近我注意到,当我用很少的写入请求测试应用程序时,我在 firestore 中的 20k 免费写入很快就结束了。经过监控,我发现我的应用程序不断地从 Firestore 写入/读取数据(虽然我看不到任何新数据正在写入。)。我在没有发出任何读/写请求的情况下监视了我的 firestore 应用程序的控制台大约一个小时,并且惊讶地发现请求的发出非常频繁,这在大约几个小时内耗尽了我的 20k 写入配额。
到目前为止,我一直试图弄清楚为什么不断地提出请求,但我无法弄清楚为什么会发生这种情况。在互联网上搜索后,我没有找到任何具体的答案,所以我很确定这是因为我碰巧犯了菜鸟错误,特别是因为这是我第一次使用 firestore 和 flutter。
读取操作:
populateItemList(){
_db.collection('users').document(merchant.uid).collection('items').getDocuments().then(
(docs){
docs.documents.forEach(
(doc){
ItemDetails item = ItemDetails(doc.data["ItemName"], doc.data["ItemPrice"], doc.data["ItemQty"], doc.data["ItemUnit"]);
listItems.add(item);
itemNameList.add(item.itemName);
}
);
listIn.add(listItems); // Sink<List<ItemDetails>>
itemNameListIn.add(itemNameList); // Sink<List<String>>
}
);
}
写操作:
setItemData(FirebaseUser user, String itemName, double itemQty, double itemPrice, String itemUnit) async{
var ref = _db.collection('users').document(user.uid).collection('items').document(itemName.toUpperCase());
Map<String, dynamic> mapToUpdate ={};
mapToUpdate = {"ItemName" : itemName.toUpperCase(), "ItemQty" : itemQty, "ItemPrice" : itemPrice, "ItemUnit" : itemUnit};
await ref.setData(mapToUpdate, merge: true);
}
为了调用写操作,我正在监听一个流,该流基本上将检查与 itemField 相关的文本字段中提供的数据是否有效。一旦被认为有效,RaisedButton 将变为活动状态并发出事件以更新信息。
StreamBuilder<bool>(
stream: _ItemUpdateFormBloc.registerValid,
builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
return RaisedButton(
child: Text('Proceed'),
onPressed: (snapshot.hasData && snapshot.data == true)
? () {
_itemUpdateBloc.emitEvent(ItemUpdateEvent(
user: widget.user,
event: ItemUpdateEventType.working,
itemUnit: _itemUnitController.text,
itemPrice: double.parse(_itemPriceController.text.toString()),
itemQty: double.parse(_itemQtyController.text.toString()),
itemName: _itemNameController.text,)
);
}
: null,
);
}
)
收到事件后,事件处理程序将执行以下操作:
Stream<ItemUpdateState> eventHandler(ItemUpdateEvent event, ItemUpdateState currentState) async* {
if (event.event == ItemUpdateEventType.working){
yield ItemUpdateState.busy();
itemService = ItemService(event.user);
await itemService.setItemDataAndGetInfo(event.user, event.itemName, event.itemQty, event.itemPrice, event.itemUnit);
yield ItemUpdateState.success();
}
}
我想减少不必要的写入和读取操作的数量,如果应用程序继续如此频繁地发出读/写请求,这将花费我一大笔钱。谢谢您的帮助。