'How return bool from write on file
Am using this for write file on phone
Future<File> writeData(data) async {
final file = await _localFile;
return file.writeAsString(data);
}
how can i know if it write successfully on file ? like is there a way to return a bool value when i write on file to know it write successfully ?
Solution 1:[1]
Failures in file writing will be errors or exceptions. You could catch them all and return false
in that case and otherwise true
.
Future<bool> writeData(data) async {
try {
final file = await _localFile;
file.writeAsString(data);
return true;
catch (_) {
return false;
}
}
Personal opinion ahead:
It would be wiser to handle those errors properly instead of returning a boolean though.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Merlin Attila Fejzuli |