dvCrashDirectoryFor function

String? dvCrashDirectoryFor({
  1. required String appId,
  2. required String os,
  3. required Map<String, String> environment,
  4. String? androidStateDirectory,
})

The directory crash records for appId go in on os, or null when the platform gives no directory that survives a restart.

DARTVEL_CRASH_DIR wins everywhere, for a deployment that mounts one. The rest is the per-user data directory each platform keeps across reboots — never the temp directory, which is wiped exactly when a crash loop ends in a restart. Null rather than a guess: a relative path is written from whatever the working directory is, which on a device is /.

androidStateDirectory is the device runtime's directory inside the application's files directory; Android sets no HOME, and the files directory is the only place an application may write.

Implementation

String? dvCrashDirectoryFor({
  required String appId,
  required String os,
  required Map<String, String> environment,
  String? androidStateDirectory,
}) {
  String? usable(String? value) {
    if (value == null) return null;
    String trimmed = value.trim();
    while (trimmed.length > 1 &&
        (trimmed.endsWith('/') || trimmed.endsWith(r'\'))) {
      trimmed = trimmed.substring(0, trimmed.length - 1);
    }
    if (trimmed.isEmpty || trimmed == '/' || trimmed == r'\') return null;
    return trimmed;
  }

  final String? declared = usable(environment['DARTVEL_CRASH_DIR']);
  if (declared != null) return declared;

  switch (os) {
    case 'android':
      final String? state = usable(androidStateDirectory);
      if (state == null) return null;
      final int slash = state.lastIndexOf('/');
      final String? files =
          slash <= 0 ? null : usable(state.substring(0, slash));
      return files == null ? null : '$files/dartvel-crashes';
    case 'ios':
    case 'macos':
      final String? home = usable(environment['HOME']);
      return home == null
          ? null
          : '$home/Library/Application Support/$appId/dartvel-crashes';
    case 'windows':
      final String? base =
          usable(environment['LOCALAPPDATA']) ?? usable(environment['APPDATA']);
      return base == null ? null : '$base\\$appId\\dartvel-crashes';
    default:
      final String? data = usable(environment['XDG_DATA_HOME']);
      if (data != null) return '$data/$appId/dartvel-crashes';
      final String? home = usable(environment['HOME']);
      return home == null ? null : '$home/.local/share/$appId/dartvel-crashes';
  }
}