You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
decky/lib/env.dart

49 lines
1.6 KiB

import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:shared_preferences/shared_preferences.dart';
const String _overrideKey = 'api_base_url_override';
String? _override;
String _fromEnv() {
final raw = dotenv.env['API_BASE_URL']?.trim() ?? '';
final url = raw.replaceAll(' ', '');
if (url.isEmpty) return 'http://localhost:3001';
return url.endsWith('/') ? url.substring(0, url.length - 1) : url;
}
/// API base URL (no trailing slash). Uses in-app override if set, else .env.
/// Strips spaces so accidental spaces don't break DNS.
String get apiBaseUrl {
if (_override != null && _override!.trim().isNotEmpty) {
final u = _override!.trim().replaceAll(' ', '');
if (u.isNotEmpty) {
return u.endsWith('/') ? u.substring(0, u.length - 1) : u;
}
}
return _fromEnv();
}
/// Load saved override from disk. Call once at startup.
Future<void> loadApiBaseUrlOverride() async {
final prefs = await SharedPreferences.getInstance();
_override = prefs.getString(_overrideKey);
}
/// Save or clear the in-app base URL override. Pass null or empty to use .env again.
Future<void> setApiBaseUrlOverride(String? value) async {
final prefs = await SharedPreferences.getInstance();
if (value == null || value.trim().isEmpty) {
await prefs.remove(_overrideKey);
_override = null;
return;
}
final u = value.trim().replaceAll(' ', '');
final withoutSlash = u.endsWith('/') ? u.substring(0, u.length - 1) : u;
await prefs.setString(_overrideKey, withoutSlash);
_override = withoutSlash;
}
/// Current default from .env (ignoring override). For display only.
String get apiBaseUrlDefault => _fromEnv();

Powered by TurnKey Linux.