import 'dart:convert'; import 'package:flutter/services.dart' show rootBundle; import 'package:shared_preferences/shared_preferences.dart'; import 'backend_environment.dart'; /// Lädt die Umgebungsliste aus der Asset-Config und persistiert, welche /// Umgebung der Nutzer gewählt hat. /// /// Bewusst wird nur die **Auswahl** gespeichert, nicht die Liste selbst: /// So wirkt eine geänderte Config (neue IP, neue Umgebung) mit dem /// nächsten App-Update, ohne dass veraltete Kopien im Gerät hängen. class EnvironmentRepository { static const _selectedKey = 'selected_backend_environment'; Future loadCatalog(String assetPath) async { final raw = await rootBundle.loadString(assetPath); final json = jsonDecode(raw); if (json is! Map) { throw const FormatException('Config ist kein JSON-Objekt.'); } return ServerCatalog.fromJson(json); } Future loadSelectedId() async { final prefs = await SharedPreferences.getInstance(); return prefs.getString(_selectedKey); } Future saveSelectedId(String id) async { final prefs = await SharedPreferences.getInstance(); await prefs.setString(_selectedKey, id); } }