How to get all products from Shopify REST API - Flutter/Dart

Viewed 159

I'm trying to fetch all products from Shopify REST API. By default, product endpoint returns only 250 products. I dig into the documentation and found out that the product endpoint also returns header response which contains link to the next page

https://shopify.dev/api/usage/pagination-rest

For now, I able to fetch the first 250 products and page_info parameter value that is used to navigate to the next link

List<ProductModel> productList = [];

  void fetchProducts() async {
    Uri url = Uri.parse("example.myshopify.com/admin/api/2022-01/products.json?limit=250");
    final response = await http.get(url);
    final responseHeader = response.headers;
    String? headerLink = responseHeader['link']; // it contains page_info parameter that help us to navigate to next value, how to make use of it to fetch all products and add it inside productList (List)

    final responseBody = jsonDecode(response.body);
    List<dynamic> products = responseBody["products"] as List<dynamic>;
    List<ProductModel> tempList = [];

    for(var product in products){
      ProductModel productModel = ProductModel.fromMap(product);
      tempList.add(productModel);
    }

      productList = tempList;
      isLoading = false;
      notifyListeners();
  }
0 Answers
Related