skip to Main Content

this is my project structure:

your_project/
|-- android/
|-- ios/
|-- lib/
|-- assets/
    |-- test0.json // **Can access**
|   |-- file1.png
|   |-- file2.json
|   |-- ...
|   |-- jsons/
|       |-- test.json // **Can't access**
|       |-- file3.json
|       |-- file4.json
|       |-- ...
|-- pubspec.yaml
|-- ...

this is my pubspec.yaml:

flutter:
  uses-material-design: true
  assets:
    - assets/
    - assets/images/
    - assets/jsons/

Reading the file:

Future<void> initSupportFiles() async {
  try {
    // Get the Application Support directory path
    Directory appSupportDir = await getApplicationSupportDirectory();
    String appSupportPath = appSupportDir.path;

    // Check if the JSON file already exists in the Application Support directory
    String jsonFilePath = '$appSupportPath/jsons/test.json';
    File jsonFile = File(jsonFilePath);

    if (await jsonFile.exists()) {
      return;
    }

    // Read the JSON file from assets directly
    ByteData data = await rootBundle.load('assets/jsons/test.json');
    final buffer = data.buffer;
    await jsonFile.writeAsBytes(buffer.asUint8List());

    print('JSON file copied to Application Support directory.');
  } catch (e) {
    print('Error copying JSON file: $e');
  }
}

My problem: I can’t access ANY files inside subfolders inside Assets (only files inside the root of Assets are accessible)

I tried doing flutter pub get, but it didn’t work. How can I fix that?

2

Answers


  1. Have you tried removing positional spaces or adding, if its incorrect, or maybe restarting your ide? if the emulator is running, try restarting that as well.

    Login or Signup to reply.
  2. In the initSupportFiles function, the getApplicationSupportDirectory function from the path_provider package returns a directory path specific to the platform If you are testing the app on an emulator or simulator, these directories might not be accessible directly.you can use rootBundle to load and access them directly without relying on platform specific directories.

    you can update you initSupportFiles function with..

    import 'package:flutter/services.dart';
    
    Future<void> initSupportFiles() async {
      try {
        // Check if the JSON file already exists in the Application Support directory
        String jsonFilePath = 'assets/jsons/test.json';
        ByteData data = await rootBundle.load(jsonFilePath);
        final buffer = data.buffer;
        List<int> bytes = buffer.asUint8List();
    
        // Write the JSON file to a specific location (e.g., Application Support directory)
        // For testing purposes, you can use a different path like the Documents directory
        Directory appSupportDir = await getApplicationSupportDirectory();
        String appSupportPath = appSupportDir.path;
        String jsonFilePathInSupport = '$appSupportPath/test.json';
    
        File jsonFile = File(jsonFilePathInSupport);
        if (await jsonFile.exists()) {
          return;
        }
        await jsonFile.writeAsBytes(bytes);
    
        print('JSON file copied to Application Support directory.');
      } catch (e) {
        print('Error copying JSON file: $e');
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search