Flutter 在flatter中一次处理大量http请求
要求:Flutter 在flatter中一次处理大量http请求,flutter,http,request,simultaneous,Flutter,Http,Request,Simultaneous,要求: static Future<bool> downloadZip(List<String> urls) { int counter = 0 for(String url in urls) { bytesForFileAtUrl(url).then((bytes){ counter++; if(bytes != null) { //Add bytes to t
static Future<bool> downloadZip(List<String> urls) {
int counter = 0
for(String url in urls) {
bytesForFileAtUrl(url).then((bytes){
counter++;
if(bytes != null) {
//Add bytes to the zip encoder
}
if(counter == urls.length) {
//close the zip encoder
//download the zip file
}
}
}
}
static Future<Uint8List> bytesForFileAtUrl(String url) async {
try {
http.Response response = await http.get(url);
return response?.bodyBytes;
} catch (e) {
print('Error getting bytes: ${e.toString()}');
return null;
}
}
我有1000个PDF URL,我想从中创建zip
功能:
static Future<bool> downloadZip(List<String> urls) {
int counter = 0
for(String url in urls) {
bytesForFileAtUrl(url).then((bytes){
counter++;
if(bytes != null) {
//Add bytes to the zip encoder
}
if(counter == urls.length) {
//close the zip encoder
//download the zip file
}
}
}
}
static Future<Uint8List> bytesForFileAtUrl(String url) async {
try {
http.Response response = await http.get(url);
return response?.bodyBytes;
} catch (e) {
print('Error getting bytes: ${e.toString()}');
return null;
}
}
static FuturedownloadZip(列表url){
int计数器=0;
forEach(async(){
等待bytesForFileAtUrl(url)。然后((字节){
计数器++;
如果(字节数!=null){
//向zip编码器添加字节
}
if(计数器==url.length){
//关闭压缩编码器
//下载zip文件
}
});
});
}
静态未来bytesForFileAtUrl(字符串url)异步{
试一试{
http.Response-Response=等待http.get(url);
返回响应?.bodyBytes;
}捕获(e){
print('获取字节时出错:${e.toString()}');
返回null;
}
}
也许可以试试这个?试试
Future.forEach
@pskink有什么例子吗?var list=[[0,5,10],[1,6,11],[2,7],[3,8],[4,9],];var futures=list.map((子列表){return Future.forEach(子列表,(i)=>Future.delayed(3.5秒,()=>print(i));});Future.wait(futures).then((打印('all done!!!');})代码>此处最多可执行5个并发作业我认为您需要扩展此答案-也许可以用英语解释此代码片段解决问题的原因,以及此片段与他们已经发布的代码的关系。
static Future < bool > downloadZip(List < String > urls) {
int counter = 0;
urls.forEach(async() {
await bytesForFileAtUrl(url).then((bytes){
counter++;
if (bytes != null) {
//Add bytes to the zip encoder
}
if (counter == urls.length) {
//close the zip encoder
//download the zip file
}
});
});
}
static Future < Uint8List > bytesForFileAtUrl(String url) async {
try {
http.Response response = await http.get(url);
return response?.bodyBytes;
} catch (e) {
print('Error getting bytes: ${e.toString()}');
return null;
}
}