Search code examples
flutterflutter-dependencies

Flutter, how to create same path directory in Android and IOS


 File('/data/user/0/com.abc/cache/full.jpg')
        .writeAsBytesSync(class_image.encodeJpg(data));

I found out that Android works with this path. But in ios, I found out that it doesn't work with the error below.

[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.abc/cache/full.jpg' (OS Error: No such file or directory, errno = 2)

I want to use the same folder for both. Is this possible? If not, the workaround is to manage the ios folder separately?


Solution

    • The storage directory of each platform is different
    • You can obtain the local storage directory separately through the path_provider plugin, and then create your path.
    • like this
      Future<Directory?> getLocalDirectory() async {
        return Platform.isAndroid
            ? await path.getExternalStorageDirectory()
            : await path.getApplicationSupportDirectory();
      }