I want to store a small bit of information in a file, persisted between runs of a command-line app. Probably the best location is a small file in the user's home directory.
I'd like to write a property/config file into a user's home directory. How can I tell, for Windows, Mac, and Linux, what the user's home directory is? I am using Dart.
Similar to other answers:
// Get the home directory or null if unknown.
String homeDirectory() {
switch (Platform.operatingSystem) {
case 'linux':
case 'macos':
return Platform.environment['HOME'];
case 'windows':
return Platform.environment['USERPROFILE'];
case 'android':
// Probably want internal storage.
return '/storage/sdcard0';
case 'ios':
// iOS doesn't really have a home directory.
return null;
case 'fuchsia':
// I have no idea.
return null;
default:
return null;
}
}
You can tweak how it behaves according to your needs. I did try and figure out the answer for Fuchsia but I can't work out if it even has home directories to be honest!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With