0

I am trying to use the following function in flutter:

Future<File> getFile(String fileName) async { final appDir = await getTemporaryDirectory(); final appPath = appDir.path; final fileOnDevice = File('$appPath/$fileName'); final rawAssetFile = await rootBundle.load(fileName); final rawBytes = rawAssetFile.buffer.asUint8List(); await fileOnDevice.writeAsBytes(rawBytes, flush: true); return fileOnDevice; } 

But somehow i keep getting this error, failed PathNotFoundException: Cannot open file, path = '/var/mobile/Containers/Data/Application/352527E9-XXXX-XXXX-XXXX-DCFXXXXXXXXX/Library/Caches/assets/

The function is called by final dataFile = await getFile('assets/sample.txt');

Does anyone know how to solve this? Any help is appreciated!

This is my flutter doctor -v

[✓] Flutter (Channel stable, 3.7.0, on macOS 13.1 22C65 darwin-arm64, locale en-SG) • Flutter version 3.7.0 on channel stable at /Users/xaviertoh/Documents/Flutter/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision b06b8b2710 (3 weeks ago), 2023-01-23 16:55:55 -0800 • Engine revision b24591ed32 • Dart version 2.19.0 • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1) • Android SDK at /Users/xaviertoh/Library/Android/sdk • Platform android-33, build-tools 33.0.1 • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14C18 • CocoaPods version 1.11.3

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] VS Code (version 1.75.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.58.0

[✓] Connected device (3 available) • iPhone (mobile) • 00008020-000D05313C02002E • ios • iOS 13.5 17F75 • macOS (desktop) • macos • darwin-arm64 • macOS 13.1 22C65 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 110.0.5481.77

[✓] HTTP Host Availability • All required HTTP hosts are available

• No issues found!

Thank you for reading!

Things i have tried:

  1. I have confirmed that my file name is correct in the assets folder and i also tried running this code in various locations in the code such as in a widget and also in main() just to test it out.

  2. I also tried running flutter in xcode and visual studio code.

  3. Also tried to run the code in android emulator and also on iPhone physical device.

0

1 Answer 1

1

The /var/mobile/Containers/Data/Application/.../Library/Caches/assets/ directory must already exist. File.writeAsBytes won't automatically create parent directories for you.

You can do that step by first doing:

await fileOnDevice.parent.create(recursive: true); 
Sign up to request clarification or add additional context in comments.

1 Comment

It helped me, I wanted to write in application directory and got exception and this was helpful, thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.