Reputation: 1
i need to load the data to my application
when i retrive from this link https://jsonplaceholder.typicode.com/albums its work but when i use rapid api not load data the problem is in the connection to the server or not
** the code file here **
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
Future<List<Data>> fetchData() async {
var uri = Uri.https('yummly2.p.rapidapi.com', '/feeds/list',
{"limit": "18", "start": "0", "tag": "list.recipe.popular"});
final response = await http
.get(uri,
headers:{
"x-rapidapi-key": "my api key",
"x-rapidapi-host": "yummly2.p.rapidapi.com",
"useQueryString": "true"
}
);
if (response.statusCode == 200) {
// If the server did return a 200 OK response,
// then parse the JSON.
List jsonResponse = json.decode(response.body);
return jsonResponse.map((data) => Data.fromJson(data)).toList();
} else {
// If the server did not return a 200 OK response,
// then throw an exception.
throw Exception('Failed to load Recipes');
}
}
class Data {
//final int image;
// final int rating;
final String name;
Data({ required this.name});
factory Data.fromJson(Map<String, dynamic> json) {
return Data(
//image: json['thumbnail_url'],
//rating: json['user_ratings']['score'],
name: json['name'],
);
}
}
Upvotes: 0
Views: 53
Reputation: 640
You pass wrong link in Uri.https(). Take an example below for how to pass API link of rapidapi
Uri.parse(
"$pre_url/everything?q=apple&from=2022-09-26&to=2022-09-26&sortBy=popularity&apiKey=$api_key"
Upvotes: 1