fetchImages function

Future<List<ImgResult>> fetchImages(
  1. String key,
  2. String token,
  3. List<String> ids,
  4. CoreGenModel modelGen,
)

Implementation

Future<List<ImgResult>> fetchImages(
  String key,
  String token,
  List<String> ids,
  CoreGenModel modelGen,
) async {
  final headers = {'X-Figma-Token': token};
  final dio = Dio();
  dio.options.queryParameters = {
    'ids': ids.join(','),
    'format': modelGen.format,
    'scale': modelGen.scale,
  };
  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
        .map((e) => ImgResult(e.key, e.value))
        .toList();
    return imgLst ?? <ImgResult>[];
  } else {
    stdout.writeln(response.statusMessage);
    return [];
  }
}