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.
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.
String datum = await DefaultAssetBundle.of(context)
.loadString("assets/files/preset_test.json");
It fixed I did the reading with this code.