applicationDirectory function

String? applicationDirectory({
  1. required String appName,
  2. required TargetPlatform platform,
  3. required Map<String, String> environment,
  4. required String temporary,
})

Where this platform keeps a small document belonging to one application.

A table rather than one path, because there is no one path. The rules below are each platform's own convention, and getting them from the environment rather than from a plugin is what keeps this package free of path_provider — the dependency policy in ARCHITECTURE.md asks for that, and on three of the five it is genuinely just an environment variable.

The two mobile platforms are the interesting ones. Neither publishes its container in the environment, but both set TMPDIR, and both put it inside that container:

  • Android: /data/user/0/<package>/cache, whose parent is the private data directory that files/ and shared_prefs/ sit in;
  • iOS: <container>/tmp, whose parent is the container.

So the parent of temporary is the root to build from. That is an assumption about the Flutter engine's own setup, not a documented API, and it is written down here rather than buried: if it ever stops holding, the symptom is a game that cannot keep settings, which is exactly what this replaced — no worse, and now with a name.

Returns null where nothing can be worked out, which a caller treats as "this platform does not keep anything" rather than as a failure.

Implementation

String? applicationDirectory({
  required String appName,
  required TargetPlatform platform,
  required Map<String, String> environment,
  required String temporary,
}) {
  final home = environment['HOME'];
  switch (platform) {
    case TargetPlatform.macOS:
      // A sandboxed application's `HOME` is its container, so this is inside it.
      return home == null ? null : '$home/Library/Application Support/$appName';
    case TargetPlatform.iOS:
      return '${_parent(temporary)}/Library/Application Support/$appName';
    case TargetPlatform.android:
      // Beside `files/`, which is where an Android application's own data goes.
      return '${_parent(temporary)}/files/$appName';
    case TargetPlatform.linux:
      final config =
          environment['XDG_CONFIG_HOME'] ??
          (home == null ? null : '$home/.config');
      return config == null ? null : '$config/$appName';
    case TargetPlatform.windows:
      final appData = environment['APPDATA'];
      return appData == null ? null : '$appData\\$appName';
    case TargetPlatform.fuchsia:
      return null;
  }
}