I’m facing an issue with loading data in my Flutter application. While it works fine when I retrieve data from a standard JSON endpoint, like https://jsonplaceholder.typicode.com/albums
, I’m unable to fetch any data when using RapidAPI. I’m unsure if this is due to a server connection issue or an error in my setup.
If anyone has encountered similar challenges with RapidAPI, I would appreciate your insights.
Here’s my code for reference:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<RecipeData>> loadRecipeData() async {
var endPoint = Uri.https('yummly2.p.rapidapi.com', '/feeds/list',
{"limit": "18", "start": "0", "tag": "list.recipe.popular"});
final response = await http.get(endPoint, headers: {
"x-rapidapi-key": "your_api_key_here",
"x-rapidapi-host": "yummly2.p.rapidapi.com",
"useQueryString": "true"
});
if (response.statusCode == 200) {
List jsonResponse = json.decode(response.body);
return jsonResponse.map((item) => RecipeData.fromJson(item)).toList();
} else {
throw Exception('Failed to load recipes');
}
}
class RecipeData {
final String title;
RecipeData({required this.title});
factory RecipeData.fromJson(Map<String, dynamic> json) {
return RecipeData(
title: json['name'],
);
}
}