Reputation: 71
I recently started programming mobile applications with flutter. I have a problem with reading a file. In practice, while the app is running I write a string to a text file (and I can also read it correctly). My need is to read from this file every time the app is accessed; therefore, in the initState () function I call the readContent () function but I get the following message: "FileSystemException: Cannot open file, path = '/var/mobile/Containers/Data/Application/F8A5A202-45C9-47F6-93C9-E4BAE2AF3C7E/Documents/counter.txt' (OS Error: No such file or directory, errno = 2)". Could you help me?
This is my code:
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
print("directory path: "+directory.path);
return directory.path;
}
Future<io.File> get _localFile async {
final path = await _localPath;
return io.File('$path/counter.txt');
}
String datas = "";
//questa funzione viene richiamata all'avvio della schermata e serve per leggere il valore del flag checkSendingReport dal file
@override
void initState() {
super.initState();
readContent().then((String value) {
print(value);
setState(() {
String data = value;
List data_file_split = data.split("#");
checkSendingReport = data_file_split[1];
check_sending_report = data_file_split[1];
});
});
}
//Questa funzione serve per leggere il file locale scritto in precenza
Future<String> readContent() async {
try {
final file = await _localFile;
// Read the file
String contents = await file.readAsString();
//Returning the contents of the file
return contents;
} catch (e) {
// If encountering an error, return
return 'Error! '+e.toString();
}
}
//questa funzione serve per scrivere il file locale
Future<io.File> writeContent(datetime) async {
final file = await _localFile;
//Setto a true la variabile flag check_sending_report
check_sending_report = true;
//Creo la stringa da inserire nel file: composta da datetime#flag
String data_file = datetime + "#" + check_sending_report.toString();
// Write the file
return file.writeAsString(data_file);
}
Upvotes: 7
Views: 29936
Reputation: 1
The following worked for me:
Check the logs for the cache path
rm -rf /Users/{user.name}/Developer/flutter/bin/cache
Rebuild the cache:
flutter doctor
flutter pub get
Rebuild Your Project:
flutter run
If necessary Check Flutter Installation:
flutter upgrade
Hope this helps.
Upvotes: 0
Reputation: 373
Opening a file for reading and writing to the end of it. The file is created if it does not already exist.
final root = await getApplicationDocumentsDirectory();
print(root.path + '/logFile.txt');
var filePath = '${root.path}/logFile.txt';
var logFile = File(filePath);
if (!logFile.existsSync()) {
logFile.create(recursive: true);
}
logFile.writeAsStringSync('${record.time}: ${record.message}\n',
mode: FileMode.append);
logFile.readAsString().then((String contents) {
print("contents is $contents");
});
final result = await OpenFile.open(filePath);
print(result.toString());
Upvotes: 0
Reputation: 55
Future<File> _getLocalFile(String pathFlie) async {
final root = await getApplicationDocumentsDirectory();
final path = root+'/'+pathFile;
return File(path).create(recursive: true);
}
Upvotes: 3
Reputation: 823
I guess this error is coming due to initially file does not exist. so, you can add a check for file exist or not
if(file.existsSync())
if File does not exist, you can create one.
new File('$path/counter.txt').create(recursive: true);
Upvotes: 4