fetchPreview function

Future<List<ImgResult>> fetchPreview(
  1. String key,
  2. String token,
  3. String id
)

Implementation

Future<List<ImgResult>> fetchPreview(
  String key,
  String token,
  String id,
) async {
  final headers = {'X-Figma-Token': token};
  final dio = Dio();
  dio.options.queryParameters = {
    'ids': id,
    'format': 'png',
  };

  final response = await dio.request<dynamic>(
    'https://api.figma.com/v1/images/$key',
    options: Options(
      method: 'GET',
      headers: headers,
    ),
  );

  if (response.statusCode == 200) {
    final imgLst = FigmaImageResponse.fromJson(response.data)
        .images
        ?.entries
        .where((e) => e.key != null && e.key is String)
        .map((e) => ImgResult(e.key!, e.value))
        .toList();
    return imgLst ?? <ImgResult>[];
  } else {
    stdout.writeln(response.statusMessage);
    return [];
  }
}