我在我的 flutter 应用程序中定义了以下流:
static Stream<String> downloadIdentifiers() async* {
try {
yield "test";
final directory = await getApplicationDocumentsDirectory();
Response response;
Dio dio = new Dio();
response = await dio.download(
MyConstants.identifiersUrl,
join(directory.path, "identifiers.json"),
onReceiveProgress: (int received, int total) {
print("$received / $total");
},
);
yield join(directory.path, "identifiers.json");
} catch (ex) {
throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
我使用https://github.com/flutterchina/dio进行下载。
我想生成有关流的下载进度的信息,但回调onReceiveProgress仅采用常规函数作为回调。
如何获取我的流上已接收/总字节数的信息?
谢谢你!
Qio*_* Wu 14
感谢詹姆斯德林的回答。在他的帮助下我终于做到了:
static Stream<String> downloadIdentifiers() async* {
StreamController<String> streamController = new StreamController();
try {
final directory = await getApplicationDocumentsDirectory();
Dio dio = new Dio();
dio.download(
MyConstants.identifiersUrl,
join(directory.path, "identifiers.json"),
onReceiveProgress: (int received, int total) {
streamController.add("$received / $total");
print("$received / $total");
},
).then((Response response) {
streamController.add("Download finished");
})
.catchError((ex){
streamController.add(ex.toString());
})
.whenComplete((){
streamController.close();
});
yield* streamController.stream;
} catch (ex) {
throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2685 次 |
| 最近记录: |