Flutter/Dart - 为什么我的 Future return 为空?

Flutter/Dart - Why does my Future return Null?

我想 return 从 Http Post 的 response.body 中提取一个字符串。但是我的代码 returns null 而不是字符串。如果我将字符串 oldname 放在 request.send() 的范围内,它会打印得很好,但我需要在调用 uploadAudio 方法时对它进行 returned。我做错了什么?

Future<String> uploadAudio({String currentuserid, String filepath}) async {

  String oldname; 
  final serverurl = "http://example.com/audiofile.php";

  var request = http.MultipartRequest('POST', Uri.parse(serverurl));
  request.fields['userid'] = currentuserid;    

  var multiPartFile = await http.MultipartFile.fromPath("audio", filepath,
      contentType: MediaType("audio", "mp4"));
        request.files.add(multiPartFile);
        request.send().then((result) async {
          http.Response.fromStream(result).then((response) {
                if (response.statusCode == 200) {
                  String serverResponse = response.body;
                  const start = "gxz";
                  const end = "zxg";
                  final startIndex = serverResponse.indexOf(start);
                  final endIndex = serverResponse.indexOf(end, startIndex + start.length);
                  oldname = serverResponse.substring(startIndex + start.length, endIndex);
                }
          });
   });
    print oldname;
    return oldname;
  }

等待你的未来:

Future<String> uploadAudio({String currentuserid, String filepath}) async {

  String oldname; 
  final serverurl = "http://example.com/audiofile.php";

  var request = http.MultipartRequest('POST', Uri.parse(serverurl));
  request.fields['userid'] = currentuserid;    

  var multiPartFile = await http.MultipartFile.fromPath("audio", filepath,
      contentType: MediaType("audio", "mp4"));
        request.files.add(multiPartFile);
        await request.send().then((result) async {
          await http.Response.fromStream(result).then((response) {
                if (response.statusCode == 200) {
                  String serverResponse = response.body;
                  const start = "gxz";
                  const end = "zxg";
                  final startIndex = serverResponse.indexOf(start);
                  final endIndex = serverResponse.indexOf(end, startIndex + start.length);
                  oldname = serverResponse.substring(startIndex + start.length, endIndex);
                }
          });
   });
    print(oldname);
    return oldname;
  }

我认为你的困惑来自于你混合使用 awaitthen()。我会建议您总体上坚持一个概念。

我已经重写了你的代码,所以它现在到处都在使用 await(也稍微清理了一下):

Future<String> uploadAudio({String currentuserid, String filepath}) async {
  const serverurl = "http://example.com/audiofile.php";

  final request = http.MultipartRequest('POST', Uri.parse(serverurl))
    ..fields['userid'] = currentuserid;

  final multiPartFile = await http.MultipartFile.fromPath("audio", filepath,
      contentType: MediaType("audio", "mp4"));

  request.files.add(multiPartFile);

  final response = await http.Response.fromStream(await request.send());
  String oldname;

  if (response.statusCode == 200) {
    final serverResponse = response.body;
    const start = "gxz";
    const end = "zxg";
    final startIndex = serverResponse.indexOf(start);
    final endIndex = serverResponse.indexOf(end, startIndex + start.length);
    oldname = serverResponse.substring(startIndex + start.length, endIndex);
  }

  print(oldname);
  return oldname;
}

如您所见,代码现在更容易阅读,没有所有嵌套的 then() 方法。