getCardByName method

Future<MtgCard> getCardByName(
  1. String name, {
  2. SearchType searchType = SearchType.exact,
  3. String? set,
})

GET /cards/named

Returns a Card based on a name search string. This method is designed for building chat bots, forum bots, and other services that need card details quickly.

name: The name to search for.

searchType: The type of search that shall be executed by the server. SearchType.exact performs a search that returns a card with the exact name. SearchType.fuzzy performs a fuzzy search. Defaults to SearchType.exact.

set: A set code to limit the search to one set.

Implementation

Future<MtgCard> getCardByName(
  String name, {
  SearchType searchType = SearchType.exact,
  String? set,
}) async {
  final url = Uri.https(
    _baseUrl,
    '/cards/named',
    <String, String?>{
      'exact': searchType == SearchType.exact ? name : null,
      'fuzzy': searchType == SearchType.fuzzy ? name : null,
      'set': set,
    }..removeWhere((_, value) => value == null),
  );
  final response = await _httpClient.get(url);

  final json = jsonDecode(response.body) as Map<String, dynamic>;

  if (response.statusCode != 200) {
    throw ScryfallException.fromJson(json);
  }

  return MtgCard.fromJson(json);
}