Unable to fetch data from API in Flutter app via RapidAPI

I’m trying to load data in my Flutter application and it’s giving me trouble. When I try to retrieve data from a standard link like JSONPlaceholder, it works smoothly. However, when I switch to using RapidAPI, I don’t receive any data. Is this an issue with server connectivity?

Here is the code I’m using:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

Future<List<RecipeData>> getData() async {
  var url = Uri.https('yummly2.p.rapidapi.com', '/feeds/list',
      {"limit": "18", "start": "0", "tag": "list.recipe.popular"});

  final result = await http
      .get(url,
      headers:{
        "x-rapidapi-key": "your_api_key",
        "x-rapidapi-host": "yummly2.p.rapidapi.com",
        "useQueryString": "true"
      }
      );

  if (result.statusCode == 200) {
    List parsedResponse = json.decode(result.body);
    return parsedResponse.map((item) => RecipeData.fromJson(item)).toList();
  } else {
    throw Exception('Error loading recipes');
  }
}

class RecipeData {
  final String title;

  RecipeData({required this.title});

  factory RecipeData.fromJson(Map<String, dynamic> json) {
    return RecipeData(
      title: json['name'],
    );
  }
}

Could the problem lie in how I’m connecting or configuring my headers?

The issue might be with how you’re handling the API response structure. RapidAPI endpoints often return data in a different format than what you’re expecting. In your case, the Yummly API likely returns an object with the recipes nested inside, not a direct array. Try modifying your parsing logic to handle the nested structure - you’ll probably need to access something like json.decode(result.body)['feed'] or similar. Also, double-check that your API key has the correct permissions for the Yummly2 endpoint. Sometimes the key works for testing but has restrictions on certain methods. I’d suggest adding some debug prints to see the exact response structure before trying to parse it.

I had a similar issue when I first started working with RapidAPI endpoints. The problem is most likely in your JSON parsing logic. RapidAPI responses often have a different structure compared to simple REST APIs like JSONPlaceholder. Your code assumes the response is a direct array, but RapidAPI typically wraps the data in additional objects. Try logging the actual response body first with print(result.body) to see the exact structure you’re receiving. You’ll probably find that the recipe data is nested under a key like ‘feed’ or ‘results’. Also make sure your actual API key is correctly inserted and has active subscription status on RapidAPI dashboard.

check if your rapidapi subscription is still active and hasn’t expired. also that useQueryString header should probably be a boolean not a string, try removing it entirely first. i’ve seen this exact issue before where the headers format was slightly off for rapidapi endpoints.