Flutter: How to get upload / download progress for http requests

后端 未结 3 1193
误落风尘
误落风尘 2020-12-01 10:17

I am writing an app that uploads an image to a server, and instead of just showing a spinner, I\'d love to be able to get progress on the status of that upload.

Ad

3条回答
  •  难免孤独
    2020-12-01 10:44

    The way that you are already using Stream means that you are not reading the whole file into memory. It's being read in as, probably, 64k chunks.

    You could intercept the stream between the producer (File) and consumer (HttpClient) with a StreamTransformer, like this:

      int byteCount = 0;
      Stream> stream2 = stream.transform(
        new StreamTransformer.fromHandlers(
          handleData: (data, sink) {
            byteCount += data.length;
            print(byteCount);
            sink.add(data);
          },
          handleError: (error, stack, sink) {},
          handleDone: (sink) {
            sink.close();
          },
        ),
      );
    ....
      await request.addStream(stream2);
    

    You should see byteCount incrementing in 64k chunks.

提交回复
热议问题