flutter 扑动无法从API获得响应

yqlxgs2m  于 2023-01-06  发布在  Flutter
关注(0)|答案(3)|浏览(158)

我在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()),
                  )
                ],
              );
            })
          ],
        ),
      ),
    );
  }
}

这是我的模型课

class RecipeModel{
  String? image;
  String? url;
  String? source;
  String? label;

  RecipeModel({this.image, this.url, this.source, this.label});
}

任何帮助对我来说都很重要谢谢你们

s6fujrry

s6fujrry1#

据我所知,您正在获取错误。原因:getAPIData()方法中缺少结束括号。

请看下面的getAPiData()方法以便更好地理解。

getAPiData() async {
    var response = await http.get(Uri.parse(url));
    Map json = jsonDecode(response.body);
    print(response.body);
}
qhhrdooz

qhhrdooz2#

也许这会对你有帮助,这不是正确的解决办法。
根据我的发现,你的API网址有问题。当我点击其他API网址时,它起作用了。
检查下面的dart pad URL
https://dartpad.dev/?id=5bd700b8a436ea4fd7443844b1a1c4e3

yzuktlbb

yzuktlbb3#

我又运行了一次应用程序,它终于显示了一个错误消息。它说我得到了错误的API ID,我使用了不同的API ID为不同的API在我的端点。

相关问题