Flutter 使用颤振从API接收数据
我使用颤振的时间不长,我不知道如何检索与下面描述相同的数据 我用API创建了一个应用程序。我收到了除画廊图片、程序和其他信息之外的所有数据 有人能告诉我如何从API中检索图库图像、程序和附加信息吗 如果你能写我能用的代码段。谢谢 "https://tripvenue.ru/api/v1/experiences/448"Flutter 使用颤振从API接收数据,flutter,dart,hybrid-mobile-app,Flutter,Dart,Hybrid Mobile App,我使用颤振的时间不长,我不知道如何检索与下面描述相同的数据 我用API创建了一个应用程序。我收到了除画廊图片、程序和其他信息之外的所有数据 有人能告诉我如何从API中检索图库图像、程序和附加信息吗 如果你能写我能用的代码段。谢谢 "https://tripvenue.ru/api/v1/experiences/448" import 'dart:convert'; ExperiencesByCityId experiencesByCityIdFromJson(String str) =>
import 'dart:convert';
ExperiencesByCityId experiencesByCityIdFromJson(String str) =>
ExperiencesByCityId.fromJson(json.decode(str));
String experiencesByCityIdToJson(ExperiencesByCityId data) =>
json.encode(data.toJson());
class ExperiencesByCityId {
ExperiencesByCityId({
this.id,
this.title,
this.promoText,
this.country,
this.city,
this.mainPhoto,
this.type,
this.languages,
this.instantBooking,
this.duration,
this.votesCount,
this.votesAvg,
this.url,
this.pricing,
this.teaserText,
this.description,
this.program,
this.additionalInfo,
this.gallery,
this.guestsMin,
this.guestsMax,
});
int id;
String title;
String promoText;
City country;
City city;
MainPhoto mainPhoto;
String type;
List<String> languages;
bool instantBooking;
int duration;
int votesCount;
double votesAvg;
String url;
Pricing pricing;
String teaserText;
String description;
List<Program> program;
List<String> additionalInfo;
List<Gallery> gallery;
int guestsMin;
int guestsMax;
factory ExperiencesByCityId.fromJson(Map<String, dynamic> json) =>
ExperiencesByCityId(
id: json["id"],
title: json["title"],
promoText: json["promo_text"],
country: City.fromJson(json["country"]),
city: City.fromJson(json["city"]),
mainPhoto: MainPhoto.fromJson(json["main_photo"]),
type: json["type"],
languages: List<String>.from(json["languages"].map((x) => x)),
instantBooking: json["instant_booking"],
duration: json["duration"],
votesCount: json["votes_count"],
votesAvg: json["votes_avg"],
url: json["url"],
pricing: Pricing.fromJson(json["pricing"]),
teaserText: json["teaser_text"],
description: json["description"],
program:
List<Program>.from(json["program"].map((x) => Program.fromJson(x))),
additionalInfo:
List<String>.from(json["additional_info"].map((x) => x)),
gallery:
List<Gallery>.from(json["gallery"].map((x) => Gallery.fromJson(x))),
guestsMin: json["guests_min"],
guestsMax: json["guests_max"],
);
Map<String, dynamic> toJson() => {
"id": id,
"title": title,
"promo_text": promoText,
"country": country.toJson(),
"city": city.toJson(),
"main_photo": mainPhoto.toJson(),
"type": type,
"languages": List<dynamic>.from(languages.map((x) => x)),
"instant_booking": instantBooking,
"duration": duration,
"votes_count": votesCount,
"votes_avg": votesAvg,
"url": url,
"pricing": pricing.toJson(),
"teaser_text": teaserText,
"description": description,
"program": List<dynamic>.from(program.map((x) => x.toJson())),
"additional_info": List<dynamic>.from(additionalInfo.map((x) => x)),
"gallery": List<dynamic>.from(gallery.map((x) => x.toJson())),
"guests_min": guestsMin,
"guests_max": guestsMax,
};
}
class City {
City({
this.id,
this.name,
});
int id;
String name;
factory City.fromJson(Map<String, dynamic> json) => City(
id: json["id"],
name: json["name"],
);
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
};
}
class Gallery {
Gallery({
this.fid,
this.uri,
this.url,
});
int fid;
String uri;
String url;
factory Gallery.fromJson(Map<String, dynamic> json) => Gallery(
fid: json["fid"],
uri: json["uri"],
url: json["url"],
);
Map<String, dynamic> toJson() => {
"fid": fid,
"uri": uri,
"url": url,
};
}
class MainPhoto {
MainPhoto({
this.id,
this.uri,
this.url,
});
int id;
String uri;
String url;
factory MainPhoto.fromJson(Map<String, dynamic> json) => MainPhoto(
id: json["id"],
uri: json["uri"],
url: json["url"],
);
Map<String, dynamic> toJson() => {
"id": id,
"uri": uri,
"url": url,
};
}
class Pricing {
Pricing({
this.type,
this.amount,
this.currency,
this.formatted,
this.groupSizeMin,
this.groupSizeMax,
});
String type;
double amount;
String currency;
String formatted;
int groupSizeMin;
int groupSizeMax;
factory Pricing.fromJson(Map<String, dynamic> json) => Pricing(
type: json["type"],
amount: json["amount"],
currency: json["currency"],
formatted: json["formatted"],
groupSizeMin: json["group_size_min"],
groupSizeMax: json["group_size_max"],
);
Map<String, dynamic> toJson() => {
"type": type,
"amount": amount,
"currency": currency,
"formatted": formatted,
"group_size_min": groupSizeMin,
"group_size_max": groupSizeMax,
};
}
class Program {
Program({
this.first,
this.second,
});
String first;
Second second;
factory Program.fromJson(Map<String, dynamic> json) => Program(
first: json["first"],
second: secondValues.map[json["second"]],
);
Map<String, dynamic> toJson() => {
"first": first,
"second": secondValues.reverse[second],
};
}
enum Second { EMPTY, SECOND, PURPLE }
final secondValues = EnumValues({
"": Second.EMPTY,
"по возможности посмотрим их на закате": Second.PURPLE,
"здесь вы сделаете классные фото на заброшке": Second.SECOND
});
class EnumValues<T> {
Map<String, T> map;
Map<T, String> reverseMap;
EnumValues(this.map);
Map<T, String> get reverse {
if (reverseMap == null) {
reverseMap = map.map((k, v) => new MapEntry(v, k));
}
return reverseMap;
}
}
导入'dart:convert';
ExperiencesByCityId experiencesByCityIdFromJson(字符串str)=>
ExperiencesByCityId.fromJson(json.decode(str));
字符串experiencesByCityIdToJson(ExperiencesByCityId数据)=>
encode(data.toJson());
课堂经验ByCityId{
经验比城市ID({
这个身份证,
这个名字,
这篇文章,
这个国家,
这个城市,
这张照片,
这个.类型,,
这就是语言,
这是即时预订,
这个时间,,
这是Votescont,
这是votesAvg,
这个.url,
这就是价格,
这个.triesterText,
这个.说明,,
这个节目,,
这是额外的信息,
这个画廊,
这是guestsMin,
这是guestsMax,
});
int-id;
字符串标题;
字符串和文本;
城乡;
城市;
主要照片主要照片;
字符串类型;
列出语言;
bool即时预订;
int持续时间;
int votescont;
双votesAvg;
字符串url;
定价;
字符串文本;
字符串描述;
列表程序;
列出其他信息;
列表库;
国际来宾;
int guestsMax;
工厂体验ByCityId.fromJson(映射json)=>
经验比城市ID(
id:json[“id”],
标题:json[“标题”],
promoText:json[“promo_text”],
国家:City.fromJson(json[“国家]),
城市:city.fromJson(json[“城市]),
mainPhoto:mainPhoto.fromJson(json[“main_photo”]),
类型:json[“类型”],
语言:List.from(json[“languages”].map((x)=>x)),
instantBooking:json[“即时预订”],
持续时间:json[“持续时间”],
votesCount:json[“票数”],
votesAvg:json[“投票”],
url:json[“url”],
定价:pricing.fromJson(json[“pricing”]),
StraterText:json[“摘要文本”],
description:json[“description”],
节目:
List.from(json[“program”].map((x)=>program.fromJson(x)),
其他信息:
List.from(json[“附加信息”].map((x)=>x)),
画廊:
List.from(json[“gallery”].map((x)=>gallery.fromJson(x)),
guestsMin:json[“guests_min”],
guestsMax:json[“guests_max”],
);
映射到JSON()=>{
“id”:id,
“头衔”:头衔,
“促销文本”:促销文本,
“国家”:country.toJson(),
“city”:city.toJson(),
“main_photo”:mainPhoto.toJson(),
“类型”:类型,
“语言”:列表.from(languages.map((x)=>x)),
“即时预订”:即时预订,
“持续时间”:持续时间,
“选票计数”:votesCount,
“平均投票数”:votesAvg,
“url”:url,
“定价”:pricing.toJson(),
“摘要文字”:摘要文字,
“描述”:描述,
“program”:List.from(program.map((x)=>x.toJson()),
“附加信息”:列表从(附加信息映射((x)=>x)),
“gallery”:List.from(gallery.map((x)=>x.toJson()),
“来宾”\u min:guestsMin,
“guests_max”:guestsMax,
};
}
阶级城市{
城市({
这个身份证,
这个名字,
});
int-id;
字符串名;
factory City.fromJson(映射json)=>City(
id:json[“id”],
名称:json[“名称”],
);
映射到JSON()=>{
“id”:id,
“姓名”:姓名,
};
}
班级画廊{
画廊({
这个,fid,
这个.uri,
这个.url,
});
int-fid;
字符串uri;
字符串url;
factory Gallery.fromJson(Map json)=>Gallery(
fid:json[“fid”],
uri:json[“uri”],
url:json[“url”],
);
映射到JSON()=>{
“fid”:fid,
“uri”:uri,
“url”:url,
};
}
班级主要照片{
主要照片({
这个身份证,
这个.uri,
这个.url,
});
int-id;
字符串uri;
字符串url;
factory MainPhoto.fromJson(映射json)=>MainPhoto(
id:json[“id”],
uri:json[“uri”],
url:json[“url”],
);
映射到JSON()=>{
“id”:id,
“uri”:uri,
“url”:url,
};
}
类别定价{
定价({
这个.类型,,
这个金额,,
这个,货币,,
这个.格式化了,,
这是我的朋友,
这个.groupSizeMax,
});
字符串类型;
双倍金额;
字符串货币;
字符串格式;
国际集团;
int-groupSizeMax;
工厂定价。fromJson(Map json)=>定价(
类型:json[“类型”],
金额:json[“金额”],
货币:json[“货币”],
格式化:json[“格式化”],
groupSizeMin:json[“group\u size\u min”],
groupSizeMax:json[“组大小最大值”],
);
映射到JSON()=>{
“类型”:类型,
“金额”:金额,
“货币”:货币,
“格式化”:格式化,
“组大小最小”:组大小最小,
“组大小最大值”:组大小最大值,
};
}
班级计划{
节目({
这,首先,,
这一秒,
});
先串;
第二;
工厂程序.fromJson(映射json)=>程序(
first:json[“first”],
second:secondValues.map[json[“second”]],
);
映射到JSON()=>{
“第一”:第一,
“秒”:秒值。反转[秒],
};
}
枚举第二个{空,第二个,紫色}
最终秒值=枚举值({
“”:秒。空,
“紫色”:第二个紫色,
“第二次:第二次
});
类枚举值{
地图;
地图反转地图;
枚举值(this.map);
地图反转{
if(reverseMap==null){
reverseMap=map.map((k,v)=>newMapEntry(v,k));
}
反向返回
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: TextButton(
onPressed: () async {
var response = await http.get(
Uri.parse('https://tripvenue.ru/api/v1/experiences/448'));
var body = response.body;
Response myResponse = Response.fromJson(json.decode(body));
print(myResponse.gallery.first.url);
},
child: Text(
"press",
),
),
)));
}
}
class Response {
int id;
String title;
String promoText;
Country country;
Country city;
MainPhoto mainPhoto;
String type;
List<String> languages;
bool instantBooking;
int duration;
int votesCount;
double votesAvg;
String url;
Pricing pricing;
String teaserText;
String description;
List<Program> program;
List<String> additionalInfo;
List<Gallery> gallery;
int guestsMin;
int guestsMax;
Response(
{this.id,
this.title,
this.promoText,
this.country,
this.city,
this.mainPhoto,
this.type,
this.languages,
this.instantBooking,
this.duration,
this.votesCount,
this.votesAvg,
this.url,
this.pricing,
this.teaserText,
this.description,
this.program,
this.additionalInfo,
this.gallery,
this.guestsMin,
this.guestsMax});
Response.fromJson(Map<String, dynamic> json) {
id = json['id'];
title = json['title'];
promoText = json['promo_text'];
country =
json['country'] != null ? new Country.fromJson(json['country']) : null;
city = json['city'] != null ? new Country.fromJson(json['city']) : null;
mainPhoto = json['main_photo'] != null
? new MainPhoto.fromJson(json['main_photo'])
: null;
type = json['type'];
languages = json['languages'].cast<String>();
instantBooking = json['instant_booking'];
duration = json['duration'];
votesCount = json['votes_count'];
votesAvg = json['votes_avg'];
url = json['url'];
pricing =
json['pricing'] != null ? new Pricing.fromJson(json['pricing']) : null;
teaserText = json['teaser_text'];
description = json['description'];
if (json['program'] != null) {
program = <Program>[];
json['program'].forEach((v) {
program.add(new Program.fromJson(v));
});
}
additionalInfo = json['additional_info'].cast<String>();
if (json['gallery'] != null) {
gallery = <Gallery>[];
json['gallery'].forEach((v) {
gallery.add(new Gallery.fromJson(v));
});
}
guestsMin = json['guests_min'];
guestsMax = json['guests_max'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['title'] = this.title;
data['promo_text'] = this.promoText;
if (this.country != null) {
data['country'] = this.country.toJson();
}
if (this.city != null) {
data['city'] = this.city.toJson();
}
if (this.mainPhoto != null) {
data['main_photo'] = this.mainPhoto.toJson();
}
data['type'] = this.type;
data['languages'] = this.languages;
data['instant_booking'] = this.instantBooking;
data['duration'] = this.duration;
data['votes_count'] = this.votesCount;
data['votes_avg'] = this.votesAvg;
data['url'] = this.url;
if (this.pricing != null) {
data['pricing'] = this.pricing.toJson();
}
data['teaser_text'] = this.teaserText;
data['description'] = this.description;
if (this.program != null) {
data['program'] = this.program.map((v) => v.toJson()).toList();
}
data['additional_info'] = this.additionalInfo;
if (this.gallery != null) {
data['gallery'] = this.gallery.map((v) => v.toJson()).toList();
}
data['guests_min'] = this.guestsMin;
data['guests_max'] = this.guestsMax;
return data;
}
}
class Country {
int id;
String name;
Country({this.id, this.name});
Country.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
return data;
}
}
class MainPhoto {
int id;
String uri;
String url;
MainPhoto({this.id, this.uri, this.url});
MainPhoto.fromJson(Map<String, dynamic> json) {
id = json['id'];
uri = json['uri'];
url = json['url'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['uri'] = this.uri;
data['url'] = this.url;
return data;
}
}
class Pricing {
String type;
double amount;
String currency;
String formatted;
int groupSizeMin;
int groupSizeMax;
Pricing(
{this.type,
this.amount,
this.currency,
this.formatted,
this.groupSizeMin,
this.groupSizeMax});
Pricing.fromJson(Map<String, dynamic> json) {
type = json['type'];
amount = json['amount'];
currency = json['currency'];
formatted = json['formatted'];
groupSizeMin = json['group_size_min'];
groupSizeMax = json['group_size_max'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['type'] = this.type;
data['amount'] = this.amount;
data['currency'] = this.currency;
data['formatted'] = this.formatted;
data['group_size_min'] = this.groupSizeMin;
data['group_size_max'] = this.groupSizeMax;
return data;
}
}
class Program {
String first;
String second;
Program({this.first, this.second});
Program.fromJson(Map<String, dynamic> json) {
first = json['first'];
second = json['second'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['first'] = this.first;
data['second'] = this.second;
return data;
}
}
class Gallery {
int fid;
String uri;
String url;
Gallery({this.fid, this.uri, this.url});
Gallery.fromJson(Map<String, dynamic> json) {
fid = json['fid'];
uri = json['uri'];
url = json['url'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['fid'] = this.fid;
data['uri'] = this.uri;
data['url'] = this.url;
return data;
}
}