Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter How can I get progress status of uploading file to google drive by using google drive api?

I am using google_apis v3 to upload a file to drive. How can I get the progress percentage of uploading status through the Google Drive API library? And also how can I cancel the uploading file?

The sample source code is as below.

  Future<File> uploadFile(
      GoogleAuthClient googleAuthClient, FileUpload fileUpload) async {
    final DriveApi driveApi = driveDataProvider.getDriveApi(googleAuthClient);

    final Stream<List<int>> mediaStream =
        Future.value(fileUpload.data).asStream().asBroadcastStream();

    Media media = Media(mediaStream, fileUpload.size);

    File createdFile = await driveApi.files.create(fileUpload.file,
        uploadMedia: media, uploadOptions:  UploadOptions.resumable;);

    return createdFile;
  }
like image 448
hayashi Avatar asked Oct 14 '25 04:10

hayashi


1 Answers

Try something like this.

int byteCount = 0;
Stream<List<int>> mediaStream = file.openRead().transform(
   StreamTransformer.fromHandlers(handleData: (data, sink) {
        byteCount += data.length;
        //add a callback function here.
        // callback(byteCount);
        sink.add(data);
      }, handleError: (error, stackTrace, sink) {
        //Handle error
      }, handleDone: (sink) {
        sink.close();
      },
   ),
);
like image 142
Werner Scholtz Avatar answered Oct 16 '25 22:10

Werner Scholtz