[英]Flutter: Cloud Firestore upload progress indicator
我看過很多關於將文件和圖像上傳到 Firebase 存儲的教程/論壇/主題,但是有沒有辦法在將數據上傳到 Cloud Firestore 時顯示進度指示器?
即我單擊上傳按鈕並顯示進度指示器。 上傳過程幾乎是瞬間完成的,因此我想添加 2-3 秒的延遲,以表明在此過程中確實發生了某些事情並且已成功完成。
我目前正在使用flutter_bloc庫來處理上傳到 Cloud Firestore。
我的小部件構建器頁面上的代碼:
onPressed: summaryDataList.length == 0
? null
: () {
//Submit data to firebase
BlocProvider.of<SummaryBloc>(context).add(
UploadSummaryToFirebase(date: fullDate),
);
//Other code
},
在 SummaryBloc 中上傳代碼:
Stream<SummaryState> _uploadSummaryToFirebase(
UploadSummaryToFirebase event) async* {
List<Map<String, dynamic>> listOfOrders = [];
var currentState = this.state;
try {
if (currentState is SummaryListState) {
currentState.summaryDataList.forEach((summaryData) async {
listOfOrders.add(summaryData.toJson());
});
var ref = Firestore.instance.collection('orders').document();
ref.setData({
'date': event.date,
'totalIncome': currentState.totalForDay,
'tenPercentOfDay': currentState.totalForDay * 0.1,
'orders': listOfOrders,
});
}
} catch (e) {
print("Error uploading, $e");
print("Error uploading to Firestore");
yield SummaryError();
}
}
這個集團有沒有辦法返回“whenComplete()”方法以顯示某種進度指示器?
有沒有辦法在將數據上傳到 Cloud Firestore 時顯示進度指示器?
對ref.setData()
調用要么不完整,要么完成。 沒有其他進展可以跟蹤。 請注意setData()是異步的並返回一個 Future。 您可以像 dart 中的任何其他 Future 一樣使用此 Future 來確定操作何時完成。 那么,也許您想await
結果,以便_uploadSummaryToFirebase
只在工作完成之前返回?
另請注意setData()
已棄用,並且在用於 Flutter 的 Firestore API 的較新版本中,您應該使用set()
(它也返回一個 Future)。
嘗試使用此方法在 Cloud-Firestore 上上傳媒體時獲取並顯示 flutter 的進度。
task.snapshotEvents.listen((TaskSnapshot event) {
var progress =
(event.bytesTransferred.toDouble() / event.totalBytes.toDouble()) *
100;
print('progress $progress');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.