Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell where the user's home directory is, in Dart?

Tags:

dart

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.

like image 369
Seth Ladd Avatar asked Aug 26 '14 04:08

Seth Ladd


1 Answers

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!

like image 100
Timmmm Avatar answered Oct 03 '22 20:10

Timmmm