getVariantsByIds method

Future<List<ProductVariant>> getVariantsByIds(
  1. List<String> idList
)

Returns a List of ProductVariant.

Returns the variants for idList, priced in the market that ShopifyLocalization.countryCode selects — the presentment price, not the shop-currency one.

Takes variant ids (gid://shopify/ProductVariant/…), unlike getProductsByIds, whose query matches ... on Product and would return empty entries for them rather than an error. Ids that don't resolve to a variant are skipped, because nodes is polymorphic and yields null.

Implementation

Future<List<ProductVariant>> getVariantsByIds(List<String> idList) async {
  if (idList.isEmpty) return [];
  final QueryOptions _options = WatchQueryOptions(
    document: gql(getVariantsByIdsQuery),
    variables: {'ids': idList, 'country': ShopifyLocalization.countryCode},
    fetchPolicy: ShopifyConfig.fetchPolicy,
  );
  final QueryResult result = await _graphQLClient!.query(_options);
  checkForError(result);

  final nodes = (result.data ?? const {})['nodes'] as List<dynamic>? ?? [];
  return [
    for (final node in nodes)
      if (node != null) ProductVariant.fromJson(node as Map<String, dynamic>),
  ];
}