Search code examples
flutterdartdio

Flutter web - send Uint8List to server


I record audio on the web with flutter_sound package. The recorded file is saved in the blob:https://myapp.com/ae64c7... path.

Now I want to upload this file to server.

When I use MultipartFile I get the following error:

MultipartFile is only supported where dart:io is available

After researching, I realized that I have to send this file to the server as Uint8List in Flutter Web How can I send this file to the server?

My current code:

        XFile file = XFile(recordedFilePath);
        Uint8List content = await file.readAsBytes();
        var dioo = Dio();
        response = await dioo.post(Endpoints.uploadUrl);

Solution

  • XFile file = XFile(recordedFilePath);
    Uint8List content = await file.readAsBytes();
    
    FormData formData = FormData.fromMap({
      "file": MultipartFile.fromBytes(content, filename: "recordedFile.mp3")
    });
    
    Response response = await Dio().post(
      Endpoints.uploadUrl,
      data: formData,
      options: Options(
        headers: {
          "Content-Type": "multipart/form-data"
        }
      )
    );