flutterdartdart-io

Unable to find file at path with File() object


Creating this file object yields an error:

var file = await File('./assets/smslant.flf').readAsString();
//PathNotFoundException (PathNotFoundException: Cannot open file, path = './assets/smslant.flf' (OS Error: No such file or directory, errno = 2))

This is an image of my project folder

I've attempted using absolute path with path_provider, I've also attempted to use a file picker. The file itself is defined in pubspec.yaml:

flutter:

  uses-material-design: true

  assets:
    - assets/unliked.jpg
    - assets/smslant.flf
    

Solution

  • Not a flutter developer, but the assets are bundled into the apk, so you won't be able to access them as a regular file.

    You can access them however using bundles:

    import 'package:flutter/services.dart' show rootBundle;
    
    Future<String> loadAsset() async {
      return await rootBundle.loadString('assets/smslant.flf');
    }
    

    Source

    Or in your case:

    import 'package:flutter/services.dart' show rootBundle;
    
    // ...
    var file = await rootBundle.loadString('assets/smslant.flf');
    

    Note: The files are read only, so you won't be able to write to them without creating a copy first.