import 'dart:convert'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; class StoredServerInfo { const StoredServerInfo({ required this.address, required this.port, required this.caCertFingerprint, }); final String address; final int port; final String caCertFingerprint; Map toJson() => { 'address': address, 'port': port, 'caCertFingerprint': caCertFingerprint, }; factory StoredServerInfo.fromJson(Map json) { return StoredServerInfo( address: json['address'] as String, port: json['port'] as int, caCertFingerprint: json['caCertFingerprint'] as String, ); } } abstract class ServerInfoStorage { Future load(); Future save(StoredServerInfo serverInfo); Future clear(); } class SecureServerInfoStorage implements ServerInfoStorage { static const _storageKey = 'server_info'; const SecureServerInfoStorage(); static const _storage = FlutterSecureStorage(); @override Future load() async { final rawValue = await _storage.read(key: _storageKey); if (rawValue == null) { return null; } final decoded = jsonDecode(rawValue) as Map; return StoredServerInfo.fromJson(decoded); } @override Future save(StoredServerInfo serverInfo) { return _storage.write( key: _storageKey, value: jsonEncode(serverInfo.toJson()), ); } @override Future clear() { return _storage.delete(key: _storageKey); } }