[英]Flutter cant get a response from API
im quite new in flutter and i wanted to build a Reciep app with Edamam Api, but when i tried to print a response, i didnt get any response and showing nothing in my debug console.我在 flutter 中很新,我想用 Edamam Api 构建一个 Reciep 应用程序,但是当我尝试打印响应时,我没有得到任何响应并且在我的调试控制台中没有显示任何内容。 i didnt know what did i do wrong since there's now error in my code.
我不知道我做错了什么,因为我的代码现在有错误。
so here is my Homepage code所以这是我的主页代码
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:medreminder/Recipe/recipe_model.dart';
import 'package:medreminder/Reminder/ui/theme.dart';
class RecipeHomePage extends StatefulWidget {
const RecipeHomePage({super.key});
@override
State<RecipeHomePage> createState() => _RecipeHomePageState();
}
class _RecipeHomePageState extends State<RecipeHomePage> {
List<RecipeModel> list = <RecipeModel>[];
final url = "https://api.edamam.com/search?q=chicken&app_id=28c786ea&app_key=318f03b71e4911e96b70feaedf22a72e&from=0&to=3&calories=591-722&health=alcohol-free";
getAPiData() async{
var response = await http.get(Uri.parse(url));
Map json = jsonDecode(response.body);
print(response.body);
@override
void initState() {
// TODO: implement initState
getAPiData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Get.isDarkMode?lightGreyColor:Colors.grey[700],
elevation: 0,
title: Text("Healthy Food Recipe"),
),
body: Container(
margin: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
child: Column(
children: [
TextField(
decoration: InputDecoration(
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20)
),
fillColor: lightGreyColor.withOpacity(0.02),
filled: true,
),
),
SizedBox(height: 15),
GridView.builder(
shrinkWrap: true,
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2, crossAxisSpacing: 5, mainAxisSpacing: 5),
itemCount: list.length,
itemBuilder: (context, i){
final x = list[i];
return Column(
children: [
Card(
child: Image.network(x.image.toString()),
)
],
);
})
],
),
),
);
}
}
and here is my model class这是我的 model class
class RecipeModel{
String? image;
String? url;
String? source;
String? label;
RecipeModel({this.image, this.url, this.source, this.label});
}
any help would mean so much to me.任何帮助对我来说意义重大。 thankyou guys
感谢你们
As per my understanding, you are getting errors.据我了解,您遇到了错误。 Reason: there is an ending bracket missing in your getAPIData() method.
原因:您的 getAPIData() 方法中缺少结束括号。
Please have a look at below getAPiData() method for better understanding.请查看下面的 getAPIData() 方法以便更好地理解。
getAPiData() async {
var response = await http.get(Uri.parse(url));
Map json = jsonDecode(response.body);
print(response.body);
}
Maybe it'll help you, it's not proper solution.也许它会帮助你,这不是正确的解决方案。
As per my finding, something wrong with your API URL. When I hit the other API URL it works.根据我的发现,您的 API URL 有问题。当我点击另一个 API URL 时,它起作用了。
check with the below dart pad URL检查下面的 dart 垫 URL
https://dartpad.dev/?id=5bd700b8a436ea4fd7443844b1a1c4e3 https://dartpad.dev/?id=5bd700b8a436ea4fd7443844b1a1c4e3
I run the app one more time, and its finally showing an error message.我再次运行该应用程序,它最终显示了一条错误消息。 and it says that i got the API ID wrong, i use a different API ID for different API in my endpoint.
它说我把 API ID 弄错了,我在端点中为不同的 API 使用了不同的 API ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.