'How to save audio as File in flutter and send it to a flask server?

I'm trying to send an audio file (.m4a) to a flask server in my flutter app. the audio file is recorded using FlutterSoundRecorder package and saved to path "audio.aac"

I have a method called Predict (inside a class called Result) that takes File as input and returns string from flask server

  Future<String> Predict(File audioFile) async {
     var stream = new http.ByteStream(audioFile.openRead());
     stream.cast();
     var length = await audioFile.length();
     print(length);

     var uri = Uri.parse('http://127.0.0.1:5000/predict');
     var request = new http.MultipartRequest("POST", uri);
     var multipartFile = new http.MultipartFile('file', stream, length,
        filename: basename(audioFile.path));

     request.files.add(multipartFile);
     request.headers["Content-Type"] = 'multipart/form-data';
     var streamedResponse = await request.send();
     var response = await http.Response.fromStream(streamedResponse);

     Map<String, dynamic> result = jsonDecode(response.body);
     predic = "${result['prediction']}";

    return predic;
    }

Now in the RecordPage.dart file I created and instance of class Result "result" and invoked the method Predict:

  var z = OpenFile.open("audio.aac") as File;
  final x = result.Predict(z) as String;

I got the following error:

  The following _CastError was thrown building RecordPage(dirty, state: 
  _RecordPageState#29c55):
  type 'Future<OpenResult>' is not a subtype of type 'File' in type cast

Any suggestions on how to send the recorded file as File to the Predict method?
or if there is another way to send an audio file to the flask server?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source