I am trying to fetch data for my Flutter application. While retrieving data from this endpoint works- https://jsonplaceholder.typicode.com/albums, I am unable to get data when utilizing RapidAPI. I’m uncertain if the issue lies with the server connection.
Here’s my code snippet:
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<RecipeData>> loadRecipes() 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": "my_api_key",
"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('Error fetching recipes');
}
}
class RecipeData {
final String title;
RecipeData({ required this.title });
factory RecipeData.fromJson(Map<String, dynamic> json) {
return RecipeData(title: json['name']);
}
}