英文:
not load data from api in rapidapi flutter App
问题
以下是您的内容的翻译:
我需要将数据加载到我的应用程序中。
当我从这个链接 https://jsonplaceholder.typicode.com/albums 检索时,它能正常工作,但当我使用 Rapid API 时,无法加载数据。问题可能在于与服务器的连接是否正常。
** 代码文件在这里 **
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": "我的 API 密钥",
"x-rapidapi-host": "yummly2.p.rapidapi.com",
"useQueryString": "true"
});
if (response.statusCode == 200) {
// 如果服务器返回了 200 OK 响应,
// 则解析 JSON。
List jsonResponse = json.decode(response.body);
return jsonResponse.map((data) => Data.fromJson(data)).toList();
} else {
// 如果服务器没有返回 200 OK 响应,
// 则抛出异常。
throw Exception('加载食谱失败');
}
}
class Data {
final String name;
Data({required this.name});
factory Data.fromJson(Map<String, dynamic> json) {
return Data(
name: json['name'],
);
}
}
请注意,我将 "my api key" 替换为了 "我的 API 密钥",以便您在代码中使用正确的密钥。如果您需要进一步的帮助,请随时告诉我。
英文:
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'],
);
}
}
答案1
得分: 1
你在Uri.https()中传入了错误的链接。以下是如何传递RapidAPI的API链接的示例:
Uri.parse(
"$pre_url/everything?q=apple&from=2022-09-26&to=2022-09-26&sortBy=popularity&apiKey=$api_key"
英文:
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"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论