listModels method
Models suitable for chat / completion, newest first. Returns an empty list for an unset / invalid key. Each provider filters out embeddings / vision / audio-only models so the dropdown isn't flooded with irrelevant entries.
Implementation
@override
Future<List<String>> listModels(String apiKey) async {
if (apiKey.isEmpty) return const [];
final res = await _http.get(
Uri.parse('$_baseUrl/models'),
headers: {
'x-api-key': apiKey,
'anthropic-version': _apiVersion,
},
).timeout(_timeout);
if (res.statusCode >= 400) return const [];
final body = jsonDecode(res.body) as Map<String, Object?>;
final data = body['data'] as List<Object?>? ?? const [];
final ids = <String>[];
for (final m in data) {
if (m is! Map<String, Object?>) continue;
final modelId = m['id'] as String?;
if (modelId != null) ids.add(modelId);
}
// Newest first — `claude-opus-4-7` should land before
// `claude-3-5-sonnet`. The API already returns this order; sort
// by id descending to normalise.
ids.sort((a, b) => b.compareTo(a));
return ids;
}