Search code examples
flutterdartfiledirectorydocument

PathNotFoundException: Cannot open file, path = '/data/user/0/com.example.future_fit/app_flutter/preset_test.tx


Future<void> readFile() async {
final Directory directory = await getApplicationDocumentsDirectory();
final File file = File('${directory.path}/preset_test.txt');

try {
  final contents = await file.readAsString();
  final List<dynamic> jsonList = jsonDecode(contents);

  List<Map<String, dynamic>> sets = [];
  for (var json in jsonList) {
    Map<String, dynamic> set = {
      'expression': json['expression'],
      'frequency': json['frequency'],
      'amplitude': json['amplitude'],
      'pulse': json['pulse'],
      'area': json['area']
    };
    sets.add(set);
  }

  setState(() {
    previousSets = sets;
  });

  print('Success: $previousSets');
} catch (e) {
  print("Error: $e");
}

}

I have an error like below while trying to read the file from the project. enter image description here

I tried this way:

   flutter pub outdated
   
   flutter pub upgrade outdated_package
   
   flutter clean
   
   flutter pub get

but this does not work for me. I also have this package path_provider: ^2.1.3 in pubspec.yaml file and

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

these permissions in the AndroidManifest.xml file.

I couldn't solve the problem. Please help me. Thank you in advance.


Solution

  • String datum = await DefaultAssetBundle.of(context)
            .loadString("assets/files/preset_test.json");
    

    It fixed I did the reading with this code.