getRandomCardAsImage method

Future<Uint8List> getRandomCardAsImage({
  1. String? query,
  2. bool? backFace,
  3. ImageVersion? imageVersion,
})

GET /cards/random?format=image

Returns an image of a single random MtgCard object.

query: Is used to filter the possible cards and return a random card from the resulting pool of cards. Supports the same fulltext search system as the main site.

backFace: If true, the back face of the card is returned. Will return a 422 if this card has no back face. Defaults to false.

imageVersion: The version of the image that shall be returned. Defaults to ImageVersion.large.

Implementation

Future<Uint8List> getRandomCardAsImage({
  String? query,
  bool? backFace,
  ImageVersion? imageVersion,
}) async {
  final url = Uri.https(
    _baseUrl,
    '/cards/random',
    <String, String?>{
      'format': 'image',
      'q': query,
      'face': backFace == true ? 'back' : null,
      'version': imageVersion?.json,
    }..removeWhere((_, value) => value == null),
  );
  final response = await _httpClient.get(url);

  if (response.statusCode != 200) {
    final json = jsonDecode(response.body) as Map<String, dynamic>;
    throw ScryfallException.fromJson(json);
  }

  return response.bodyBytes;
}