Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter async function await not awaiting

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

–1–

–3–

–2–

This indicates that await function not await, What’s the reason?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading