How to check if file on external storage exists?

3,132 views
Skip to first unread message

Sandi

unread,
Jan 1, 2019, 2:41:39 PM1/1/19
to Flutter Dev
Hi.
I have a procedure which prepares data and writes it to external storage of Android device. After creating a file, I want to check if file really exists there, and possibly get size of it. But, I can't find a correct way to check if file exists there. So, what is corrrect way to check if file with predefined name exists? 
Here is my procedure: (problem is in a row if (outputFile.exists()) { // problem!!!!!

Thank you!

void saveToLocalFile() async {
bool accessGranted = false;
File outputFile;
var invlist = await getInventoryItem();
bool res = await SimplePermissions.checkPermission(
Permission.WriteExternalStorage);
if (res == false) {
await SimplePermissions.requestPermission(
Permission.WriteExternalStorage);
}
accessGranted = await SimplePermissions.checkPermission(
Permission.WriteExternalStorage);
if (accessGranted == true) {
final dir = await getExternalStorageDirectory();
final path = dir.path;
outputFile = File('$path/aisscanning/' + teFileName.text);
String csvoutput = const ListToCsvConverter().convert(invlist);
outputFile.writeAsString(csvoutput);
if (outputFile.exists()) { // problem!!!!!
_showDialog("Success", "File " + teFileName.text + " is prepared!");
} else {
_showDialog(
"Unsuccessful", "File " + teFileName.text + " was NOT prepared!");
}
} else {
_showDialog(
"Unsuccessful", "File " + teFileName.text + " was NOT prepared!");
}
}

EthicCoders Apps

unread,
Jan 2, 2019, 3:13:05 AM1/2/19
to Sandi, Flutter Dev
If I understnand your problem correctly -  the line outputFile.exists() function returns a Future<bool> so just replace 'your problem' line 
if (outputFile.exists()) { // problem!!!!!
with the below two lines...
bool fileExists = await outputFile.exists();
if (fileExists) { /
..
....
}

Does it help? 

--
You received this message because you are subscribed to the Google Groups "Flutter Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sandi

unread,
Jan 2, 2019, 3:26:28 AM1/2/19
to Flutter Dev
Yes, thank you very much for support. I succesfully create file on SD card, ie. External storage. 
But, this file is on Android device visible in File Explorer, but not in File Explorer on PC. Other files are just fine there. File I create is visible only after I reboot Android device. Do I need to somehow close file or something like that?

Best regards,
Sandi

Dne sreda, 02. januar 2019 09.13.05 UTC+1 je oseba EthicCoders Apps napisala:
Reply all
Reply to author
Forward
0 new messages