@@ -24,6 +24,7 @@ import 'package:google_fonts/google_fonts.dart';
2424import 'package:keyboard_dismisser/keyboard_dismisser.dart' ;
2525import 'package:logger/logger.dart' ;
2626import 'package:mobile_app_privacy/mobile_app_privacy.dart' ;
27+ import 'package:path/path.dart' as path;
2728import 'package:path_provider/path_provider.dart' ;
2829import 'package:window_size/window_size.dart' ;
2930
@@ -98,6 +99,44 @@ void main(List<String> args) async {
9899
99100 if (Util .isDesktop && args.length == 2 && args.first == "-d" ) {
100101 StackFileSystem .setDesktopOverrideDir (args.last);
102+ } else if (Platform .isLinux) {
103+ // Flatpak detection: use XDG_DATA_HOME instead of ~/.stackwallet.
104+ final flatpakId = Platform .environment['FLATPAK_ID' ];
105+ if (flatpakId != null || File ('/.flatpak-info' ).existsSync ()) {
106+ // Resolve the persistent data root. Prefer XDG_DATA_HOME when set.
107+ // Otherwise fall back to $HOME/.local/share, but only if HOME is
108+ // available. If neither is set we leave the data dir unchanged and
109+ // let StackFileSystem use its default, rather than crashing.
110+ final home = Platform .environment['HOME' ];
111+ final xdgDataHome = Platform .environment['XDG_DATA_HOME' ] ??
112+ (home != null ? path.join (home, '.local' , 'share' ) : null );
113+
114+ if (xdgDataHome != null ) {
115+ final flatpakDataDir =
116+ path.join (xdgDataHome, AppConfig .appDefaultDataDirName);
117+
118+ // Migration: move legacy data from $HOME/.stackwallet into the new
119+ // location, but only when the legacy dir exists and the new one does
120+ // not. Best-effort: never crash if the move fails.
121+ if (home != null ) {
122+ final legacyDir = Directory (
123+ path.join (home, '.${AppConfig .appDefaultDataDirName }' ),
124+ );
125+ final newDir = Directory (flatpakDataDir);
126+ if (legacyDir.existsSync () && ! newDir.existsSync ()) {
127+ try {
128+ await Directory (xdgDataHome).create (recursive: true );
129+ await legacyDir.rename (newDir.path);
130+ } catch (_) {
131+ // If rename fails (e.g. different filesystem), fall back to
132+ // using the new path anyway. The user can manually move data.
133+ }
134+ }
135+ }
136+
137+ StackFileSystem .setDesktopOverrideDir (flatpakDataDir);
138+ }
139+ }
101140 }
102141
103142 final loadCoinlibFuture = loadCoinlib ();
0 commit comments