I have an async function to upload a video using MultipartRequest:
static Future<bool> uploadVideo(File video) async {
var uri = Uri.parse('https://example.com/uploadFile');
Map<String, String> headers = {
"Content-Type": "application/json",
'Accept': 'application/json',
};
var request = http.MultipartRequest("POST", uri);
request.headers.addAll(headers);
Uint8List data = await video.readAsBytes();
List<int> list = data.cast();
request.files.add(
await http.MultipartFile.fromBytes('File', list, filename: '.mp4'),
);
final response = await request.send();
if (response.statusCode == 200) {
return true;
}
return false;
when I call the function by:
Future<List<File>> files;
print("--1--");
files.then((videos) {
videos.forEach((video) async {
// this call must be await but is not
await uploadVideo(video);
});
print("--2--");
});
print("--3--");
The console show:
--1--
--3--
--2--
This indicates that await function not await, What's the reason?
CodePudding user response:
use this code
List<File> videos = await files;
print("--1--");
for (var video in videos) {
await uploadVideo(video);
print("--2--");
}
print("--3--");
}
--1-- wating files
--2-- uploading video (--2-- Will be printed several times )
--3-- finished