How can I solve FileSystemException: Cannot retrieve length of file, path ='...' (OS Error: No such file or directory, errno = 2)


In Flutter, you can't access files directly from the project. You need to add them to an assets folder (typically assets) and also to pubspec.yaml. Then, instead of using File to read them, you use rootBundle (or one of the Asset classes).

var multipartFile = http.MultipartFile.fromBytes(
  (await rootBundle.load('assets/anatomy.jpg')).buffer.asUint8List(),
  filename: 'anatomy.jpg', // use the real name if available, or omit
  contentType: MediaType('image', 'jpg'),


While testing your API, you may find it easier to just create a Dart command line project, where you do have access to Files in the project folders.

Author by


Happy Coding ^^

Updated on December 17, 2022


  • DCodes
    DCodes 6 days

    I am trying to send a user model from dart to the api where my file is set as "IFormFile" data type in my c# backend. I tried using the multipart request but all i get is the error stated , i can't understand why it cannot retrieve the length of file.

    This is my code:

    updateUser() async {
    var uri = Uri.parse('http://myIP:8070/api/Users');
    var request = http.MultipartRequest('Put', uri);
    request.fields['email']="[email protected]";
    File imageFile = new File('Anatomy_of_a_Sunset-2.jpg');
    var stream = new http.ByteStream(DelegatingStream.typed(imageFile.openRead()));
    var length = await imageFile.length();
    var multipartFile = new http.MultipartFile('file', stream, length,
        filename: basename(imageFile.path));
    Map<String, String> headers = {
      "content-type": "application/json"
    http.StreamedResponse response = await request.send();

    Any help would be appreciated.

    This picture shows where my file is located

    • Richard Heap
      Richard Heap almost 3 years
      1 - your content type isn't JSON, (it's multipart), so omit that header. 2 - there's another name constructor for MultipartFile that is better when expecting a file - it will save you two or three lines. 3 - the error message is telling you that the file "Anatomy..." doesn't exist. It must be in a different folder or called a different name. Where is it?
    • DCodes
      DCodes almost 3 years
      Alright, I changed it to "content-type": "multipart/form-data" now, the Anatomy file is in the same directory as pubspec.yaml (beside it), and regarding the constructor i tried a lot and many were giving errors i didn't know how to fix that's why i sticked to it. @RichardHeap
    • Richard Heap
      Richard Heap almost 3 years
      Is this a dart app? Or a flutter app? If the latter, you can access files like that. You'd need to move it to assets and load it. As is easier to test this in a pure dart app, create a new project. If you didn't already, download the dart SDK (2.7.1) and point your ide's dart plugin to it.
    • DCodes
      DCodes almost 3 years
      It is a flutter app, i added a link to my post to see where the file is located. I also tried previously adding an assets folder that is shown in the screenshot and adding the file instead it. Following that i loaded it from the pubspec.yaml file and had it as new File('assets/Anatomy_of_a_Sunset-2.jpg'); but still the same error. @RichardHeap
  • DCodes
    DCodes almost 3 years
    I got this now :415 Unsupported Media Type client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format. I have the profilePicture as IFormFile in my c# backend
  • Richard Heap
    Richard Heap almost 3 years
    Get it working using Postman first (see… etc). Then update the question with a screenshot of the working Postman request.
  • Anirudh Sharma
    Anirudh Sharma over 2 years
    Is there Anu way I can send no file in it and still pass the file field in it.When I am passing null in the parameter it's giving me error.