颤音等待直到功能回归才等待

发布于 2025-02-11 22:52:24 字数 1139 浏览 1 评论 0原文

我试图找到答案,但我的问题仍然存在。

在我的异步上传函数中,我以图像的名称和生成的名称返回,我想用它来制作数据库请求。

这是我的上传函数:

Future<String> upload(File imageFile) async {
    var stream =
        new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
    // get file length
    var length = await imageFile.length();

    var uri = Uri.parse("http://localhost:8080/upload");

    var request = new http.MultipartRequest("POST", uri);

    var multipartFile = new http.MultipartFile('file', stream, length,
        filename: basename(imageFile.path));

    request.files.add(multipartFile);

    var response = await request.send();
    print(response.statusCode);

    var createdFileName = "";
    response.stream.transform(utf8.decoder).listen((value) {
      createdFileName = value;
      print(createdFileName);
    });
    return createdFileName;
  }

我这样称呼它:

 List createdFileNames = [];
      for (var e in imagefiles) {
          createdFileNames.add(await upload(File(e)));
      }

我不知道为什么,但是CreatefileNames是[“”,“”],但是上传给出了正确的名称。在调试模式下,我可以看到,循环不会等到上传完成。

你有什么建议吗?

非常感谢!

I tried to find an answer but my problem is still there.

In my asynchronous upload function I return at the and the generated name of the image, which I want to use to make my database request.

This is my upload function:

Future<String> upload(File imageFile) async {
    var stream =
        new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
    // get file length
    var length = await imageFile.length();

    var uri = Uri.parse("http://localhost:8080/upload");

    var request = new http.MultipartRequest("POST", uri);

    var multipartFile = new http.MultipartFile('file', stream, length,
        filename: basename(imageFile.path));

    request.files.add(multipartFile);

    var response = await request.send();
    print(response.statusCode);

    var createdFileName = "";
    response.stream.transform(utf8.decoder).listen((value) {
      createdFileName = value;
      print(createdFileName);
    });
    return createdFileName;
  }

I call it like this:

 List createdFileNames = [];
      for (var e in imagefiles) {
          createdFileNames.add(await upload(File(e)));
      }

I don't know why, but the createdFileNames are ["",""], but the upload gives as result the right name. In debug mode I can see, that the loop does not wait until the upload has finished.

Do you have any suggestions?

Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

俏︾媚 2025-02-18 22:52:24
response.stream.transform(utf8.decoder).listen((value) {
  createdFileName = value;
  print(createdFileName);
});

您功能中的此部分是异步的,它使用回调。

但是您不会等待任何形式完成。您只需继续返回CreateFileName,到那时很可能还没有填写。

我不知道您的流是什么样子,如果您只需要第一个值,则可以等待而不是听:

createdFileName = await  response.stream.transform(utf8.decoder).first;
response.stream.transform(utf8.decoder).listen((value) {
  createdFileName = value;
  print(createdFileName);
});

This part in your function is asynchronous, it uses a callback.

But you don't wait for it to finish in any form. You just continue to return the createdFileName, that by that time most likely has not been filled.

I don't know what your stream looks like, if you only need the first value, you could await that instead of listening:

createdFileName = await  response.stream.transform(utf8.decoder).first;
舟遥客 2025-02-18 22:52:24

替换

 response.stream.transform(utf8.decoder).listen((value) {
      createdFileName = value;
      print(createdFileName);
    });

createdFileName=await response.stream.bytesToString();

Replace

 response.stream.transform(utf8.decoder).listen((value) {
      createdFileName = value;
      print(createdFileName);
    });

with

createdFileName=await response.stream.bytesToString();
哆兒滾 2025-02-18 22:52:24

更改代码

for (var e in imagefiles) {
           upload(File(e)).then((value) => createdFileNames.add(value));
     }

change code

for (var e in imagefiles) {
           upload(File(e)).then((value) => createdFileNames.add(value));
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文