PathNotFoundException:无法打开文件,路径='/data/user/0/com.example.future_fit/app_flutter/preset_test.tx

问题描述 投票:0回答:1
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");
}

}

尝试从项目读取文件时出现如下错误。 enter image description here

我试过这样:

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

但这对我不起作用。我在 pubspec.yaml 文件中也有这个包

path_provider: ^2.1.3
并且

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

AndroidManifest.xml 文件中的这些权限。

我无法解决问题。请帮我。预先感谢您。

flutter dart file directory document
1个回答
0
投票
String datum = await DefaultAssetBundle.of(context)
        .loadString("assets/files/preset_test.json");

它修复了我用这段代码进行阅读的问题。

© www.soinside.com 2019 - 2024. All rights reserved.