[英]Flutter cant get a response from API
我在 flutter 中很新,我想用 Edamam Api 構建一個 Reciep 應用程序,但是當我嘗試打印響應時,我沒有得到任何響應並且在我的調試控制台中沒有顯示任何內容。 我不知道我做錯了什么,因為我的代碼現在有錯誤。
所以這是我的主頁代碼
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()),
)
],
);
})
],
),
),
);
}
}
這是我的 model class
class RecipeModel{
String? image;
String? url;
String? source;
String? label;
RecipeModel({this.image, this.url, this.source, this.label});
}
任何幫助對我來說意義重大。 感謝你們
也許它會幫助你,這不是正確的解決方案。
根據我的發現,您的 API URL 有問題。當我點擊另一個 API URL 時,它起作用了。
檢查下面的 dart 墊 URL
我再次運行該應用程序,它最終顯示了一條錯誤消息。 它說我把 API ID 弄錯了,我在端點中為不同的 API 使用了不同的 API ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.