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");
}
}
我试过这样:
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 文件中的这些权限。
我无法解决问题。请帮我。预先感谢您。
String datum = await DefaultAssetBundle.of(context)
.loadString("assets/files/preset_test.json");
它修复了我用这段代码进行阅读的问题。