fetchSettings method

Future<Map<String, RemoteSetting>> fetchSettings({
  1. bool force = false,
})

Fetch settings from the remote endpoint.

If force is true the local cache is ignored and a fresh request is made. Returns the full map of remote settings.

Implementation

Future<Map<String, RemoteSetting>> fetchSettings({bool force = false}) async {
  if (!force && _remote.isNotEmpty && !_hasExpired()) {
    return Map.unmodifiable(_remote);
  }

  try {
    _httpClient ??= HttpClient();
    final request = await _httpClient!.getUrl(Uri.parse(config.endpoint));
    final response = await request.close();

    if (response.statusCode != 200) {
      if (config.fallbackToLocal) return _loadFromCache();
      throw RemoteSettingsException(
        'Fetch failed with status ${response.statusCode}',
      );
    }

    final body = await response.transform(utf8.decoder).join();
    final decoded = json.decode(body) as Map<String, dynamic>;
    _applyPayload(decoded);
    await _saveToCache();
    _changesController.add(Map.unmodifiable(_remote));
    return Map.unmodifiable(_remote);
  } on SocketException {
    if (config.fallbackToLocal) return _loadFromCache();
    rethrow;
  }
}