Search code examples
flutterdart

Convert MultipartFile to XFile or Unit8List in Flutter?


I need to compress images in a layer of my app, however, in this layer I only have access to MultipartFile of that image so I need to convert it to XFile or Unit8List so I can do this in Web as well as mobile.


Solution

  • try this:

    import 'package:http/http.dart' as http;
    
    Future<XFile> multipartFileToXFile(http.MultipartFile file) async {
      final bytes = await multiPartFile.finalize().expand((element) => element).toList();
    
      final xFile = XFile.fromData(bytes, name: file.filename ?? "unknown_file");
      return xFile;
    }