当我从 Flutter 中的 API 获取数据时出现问题。请帮我获取数据并显示它。我这样的数据[关闭]
Posted
技术标签:
【中文标题】当我从 Flutter 中的 API 获取数据时出现问题。请帮我获取数据并显示它。我这样的数据[关闭]【英文标题】:I have a problem with when I got data from API in Flutter. Please help me to get data and display it. My data like this [closed] 【发布时间】:2021-10-04 23:52:18 【问题描述】:
"success": true,
"data":
"name":
"uz": "tez kunlarda Dota haqida anime o'zbek tilida",
"ru": "скоро аниме по мотивом игры дота на узбекском языке"
,
"description":
"uz": "E'lon bilan bir qatorda ijodkorlar",
"ru": "Вместе с анонсом создатели"
,
"videoLink": " ",
"date": "2021-03-15T12:09:29.653Z",
"status": true,
"_id": "604ff84e6a914c2f1dfa9a6a",
"slug": "3383590",
"image": "/public/uploads/cinema/8c926c1344dae65a03dcdbf1acb71a10.jpg",
"__v": 0
【问题讨论】:
您好,欢迎来到 SO,请查看 SO 指南以询问问题***.com/help/how-to-ask 【参考方案1】:你可以用这个https://app.quicktype.io/,很简单,把你的api答案和选择dart,放在一个文件里,你就可以轻松访问你的数据了 当你格式化你的 json 文件时,你会得到这个:
"success":true,
"data":
"name":
"uz":"tez kunlarda Dota haqida anime o'zbek tilida",
"ru":"скоро аниме по мотивом игры дота на узбекском языке"
,
"description":
"uz":"E'lon bilan bir qatorda ijodkorlar",
"ru":"Вместе с анонсом создатели"
,
"videoLink":" ",
"date":"2021-03-15T12:09:29.653Z",
"status":true,
"_id":"604ff84e6a914c2f1dfa9a6a",
"slug":"3383590",
"image":"/public/uploads/cinema/8c926c1344dae65a03dcdbf1acb71a10.jpg",
"__v":0
我给你的链接会给你这个答案:别忘了在this前面加上required。
// To parse this JSON data, do
//
// final *** = ***FromJson(jsonString);
import 'dart:convert';
*** ***FromJson(String str) => ***.fromJson(json.decode(str));
String ***ToJson(*** data) => json.encode(data.toJson());
class ***
***(
this.success,
this.data,
);
bool success;
Data data;
factory ***.fromJson(Map<String, dynamic> json) => ***(
success: json["success"],
data: Data.fromJson(json["data"]),
);
Map<String, dynamic> toJson() =>
"success": success,
"data": data.toJson(),
;
class Data
Data(
this.name,
this.description,
this.videoLink,
this.date,
this.status,
this.id,
this.slug,
this.image,
this.v,
);
Description name;
Description description;
String videoLink;
DateTime date;
bool status;
String id;
String slug;
String image;
int v;
factory Data.fromJson(Map<String, dynamic> json) => Data(
name: Description.fromJson(json["name"]),
description: Description.fromJson(json["description"]),
videoLink: json["videoLink"],
date: DateTime.parse(json["date"]),
status: json["status"],
id: json["_id"],
slug: json["slug"],
image: json["image"],
v: json["__v"],
);
Map<String, dynamic> toJson() =>
"name": name.toJson(),
"description": description.toJson(),
"videoLink": videoLink,
"date": date.toIso8601String(),
"status": status,
"_id": id,
"slug": slug,
"image": image,
"__v": v,
;
class Description
Description(
this.uz,
this.ru,
);
String uz;
String ru;
factory Description.fromJson(Map<String, dynamic> json) => Description(
uz: json["uz"],
ru: json["ru"],
);
Map<String, dynamic> toJson() =>
"uz": uz,
"ru": ru,
;
然后,当您从服务器获得正确答案时,您需要执行以下操作:
//r is my api response
if (r.statusCode >= 200 && r.statusCode < 300)
final String responseString = r.body;
*** res = ***FromJson(responseString);
// get what you want with res.<thing you want to access>
【讨论】:
我需要返回 Future> 的类型。你能帮我吗 如果你想要一个 *** 列表,你需要多个 http 请求,因为一个请求就是一个 ***。 如何在屏幕上显示这个 您从 API 获得的信息?以上是关于当我从 Flutter 中的 API 获取数据时出现问题。请帮我获取数据并显示它。我这样的数据[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
当我通过 API 从 laravel 中的数据库中获取用户数据时出现错误
Flutter - 无法将所有 json 数据从 api 响应保存到 Iterable List 编辑:(无法从可迭代列表中获取数据)