Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Cloud Firestore upload progress indicator

I've seen many tutorials/forums/threads about uploading files and images to Firebase Storage, but is there a way to show a progress indicator when uploading data to Cloud Firestore?

i.e. I click the upload button and a progress indicator shows. The upload process is almost instantaneous, so I'd like to add a 2-3 second delay just to show a indication that something actually happened during this process and that it was completed successfully.

I'm currently using the flutter_bloc library to handle the uploads to Cloud Firestore.

Code on my widget-builder page:

onPressed: summaryDataList.length == 0
                        ? null
                        : () {
                            //Submit data to firebase
                            BlocProvider.of<SummaryBloc>(context).add(
                              UploadSummaryToFirebase(date: fullDate),
                            );

                            //Other code
                          },

Upload code in 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();
}
}

Is there a way for this bloc to return a "whenComplete()" method in order to display a progress indicator of some kind?

like image 903
Michael Tolsma Avatar asked Aug 09 '26 07:08

Michael Tolsma


2 Answers

Try this to get and show progress in flutter while uploading media on cloud-Firestore.

task.snapshotEvents.listen((TaskSnapshot event) {
  var progress =
      (event.bytesTransferred.toDouble() / event.totalBytes.toDouble()) *
          100;
  print('progress $progress');
});
like image 155
Dhiren Avatar answered Aug 11 '26 23:08

Dhiren


is there a way to show a progress indicator when uploading data to Cloud Firestore?

A call to ref.setData() is either incomplete or complete. There is no other progress to track. Notice that setData() is asynchronous and returns a Future. You can use this Future, just like any other Future in dart, to determine when the operation is complete. So, maybe you want to await the result so that _uploadSummaryToFirebase only returns until the work is complete?

Also note that setData() is deprecated, and in newer versions of the Firestore API for Flutter, you should use set() (which also returns a Future).

like image 30
Doug Stevenson Avatar answered Aug 11 '26 22:08

Doug Stevenson