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

Time:01-08

 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?

CodePudding user response:

File('/data/user/0/com.abc/cache/full.jpg') I have observed that you have specified path manually. The path you speicified works only for android but for ios it is completely different.So instead of specifying manually use path provider and create platform specified path then use it

import 'package:path_provider/path_provider.dart';
Future<Directory?> getLocalDirectory() async {
return Platform.isAndroid ? await path.getTemporaryDirectory(): await path.getApplicationSupportDirectory();  }

CodePudding user response:

  • 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();
  }
  • Related