Flutter package: Not able to read file from assets

68 views Asked by At

I have few resources in my flutter package 'packme' which is used by multiple apps.

There are some resources(contains images and PDF files) which are required to be loaded within my packages and served to app in bytes. I am trying multiple ways to load data but not successful.

I have added file to packme/assets/images/test-image-01.png as well packme/lib/assets/images/test-image-01.png

packme/pubspec.yaml:

flutter:

  # To add assets to your package, add an assets section, like this:
   assets:
     - assets/images/
     - lib/assets/images/

Having following function to test path exists or not, prints false for all:

void checkPaths() {
  List<String> images = [
    'packages/packme/assets/images/test-image-01.png',
    'images/test-image-01.png',
    'lib/assets/images/test-image-01.png',
  ];

  for (var image in images) {
    print(
        "fileExists? $image ${File(image).existsSync()}"); //Prints false for each file
   }

What is the way to load bytes data for file from assets or lib from a package?

I hev checked multiple posts and flutter-docs but it doesn't work.

1

There are 1 answers

2
Vishnu On BEST ANSWER

Surprisingly File('packages/packme/assets/images/test-image-01.png').existSync() is giving false but following code works fine:

ByteData byteData = await rootBundle.load('packages/packme/assets/images/test-image-01.png');
  return byteData.buffer.asUint8List();

Can anyone reflect on this?