whitebear
whitebear

Reputation: 12461

Cannot open file from assets

This is my code.

File('assets/i18n/en/strings.xml').readAsString().then((String contents) {
  print(contents);
});

strings.xml is located at this path.

in pubspec.yaml

flutter:
  assets:
    - assets/i18n/en/strings.xml

However, I have this error.

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: FileSystemException: Cannot open file, path = 'assets/i18n/en/strings.xml' (OS Error: No such file or directory, errno = 2)

Is there any places I need to check to use File class?

Upvotes: 1

Views: 1414

Answers (1)

creativecreatorormaybenot
creativecreatorormaybenot

Reputation: 126964

You need to use AssetBundle if you want to work with assets in Flutter:

import 'package:flutter/services.dart' show rootBundle;

rootBundle.loadString('assets/i18n/en/strings.xml').then((String contents) {
  print(contents);
});

If you want to learn more about it, you can follow the official article on assets in Flutter.

Upvotes: 1

Related Questions