不从 API 在 RapidAPI Flutter 应用程序中加载数据

huangapple go评论58阅读模式
英文:

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 &#39;dart:convert&#39;;

import &#39;package:flutter/material.dart&#39;;
import &#39;package:http/http.dart&#39; as http;

Future&lt;List&lt;Data&gt;&gt; fetchData() async {
  var uri = Uri.https(&#39;yummly2.p.rapidapi.com&#39;, &#39;/feeds/list&#39;,
      {&quot;limit&quot;: &quot;18&quot;, &quot;start&quot;: &quot;0&quot;, &quot;tag&quot;: &quot;list.recipe.popular&quot;});

  final response = await http
      .get(uri,
      headers:{
        &quot;x-rapidapi-key&quot;: &quot;my api key&quot;,
        &quot;x-rapidapi-host&quot;: &quot;yummly2.p.rapidapi.com&quot;,
        &quot;useQueryString&quot;: &quot;true&quot;
      }
      );

  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) =&gt; Data.fromJson(data)).toList();
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception(&#39;Failed to load Recipes&#39;);
  }
}

class Data {
  //final int image;
 // final int rating;
  final String name;

  Data({ required this.name});

  factory Data.fromJson(Map&lt;String, dynamic&gt; json) {
    return Data(
      //image: json[&#39;thumbnail_url&#39;],
      //rating: json[&#39;user_ratings&#39;][&#39;score&#39;],
      name: json[&#39;name&#39;],
    );
  }
}


答案1

得分: 1

你在Uri.https()中传入了错误的链接。以下是如何传递RapidAPI的API链接的示例:

Uri.parse(
          "$pre_url/everything?q=apple&amp;from=2022-09-26&amp;to=2022-09-26&amp;sortBy=popularity&amp;apiKey=$api_key"
英文:

You pass wrong link in Uri.https(). Take an example below for how to pass API link of rapidapi

Uri.parse(
      &quot;$pre_url/everything?q=apple&amp;from=2022-09-26&amp;to=2022-09-26&amp;sortBy=popularity&amp;apiKey=$api_key&quot;

huangapple
  • 本文由 发表于 2023年4月20日 09:29:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76059914.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定