Home > database >  Returning a value from a response stream inside an upload function
Returning a value from a response stream inside an upload function

Time:02-10

I've created a function which uploads an imageFile to Node.js server & AWS Bucket

If I call response.stream.transform(utf8.decoder).listen((value) async {} the value is equal to the CloudFront URL where my picture is stored

I'm trying to extract this URL from my Upload function to use it in my app but I can't find how :


  Future<String> upload(File imageFile) async {
    String url = '';

[...] // some code

    // add file to multipart
    request.files.add(multipartFile);

    // send
    var response = await request.send();
   
    // listen for response
    response.stream.transform(utf8.decoder).listen((value) async {
      print(value); // prints the URL

      url = value; // this value is the url where my picture is stored 
// I'd like to use it outside this function 
// I thought of returning it but I get an empty string
    });
 
    return url;
  }

CodePudding user response:

response.stream is a ByteStream. Rather than calling listen, call bytesToString - details here.

So you replace the whole transform section with:

return await response.stream.bytesToString();
  • Related