Flutter —— 网络请求
1.pubspec.yaml
dio: ^3.0.9 #网络请求
2.使用
var url = "urlurlurlurlurl";
Response response = await Dio().get(url);
var json = jsonDecode(response.toString());
var ks = HomeModel.fromJson(json);
if (ks.errorNo == "0") {
setState(() {
model = ks;
});
}
}
3.对象的使用
class HomeModel {
Data? data;
String? errorInfo;
String? errorNo;
HomeModel({this.data, this.errorInfo, this.errorNo});
HomeModel.fromJson(Map<String, dynamic> json) {
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
errorInfo = json['errorInfo'];
errorNo = json['errorNo'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.data != null) {
data['data'] = this.data!.toJson();
}
data['errorInfo'] = this.errorInfo;
data['errorNo'] = this.errorNo;
return data;
}
}
class Data {
List<ImageAdList>? imageAdList;
List<ModuleList>? moduleList;
List<NewsList>? newsList;
Data({
this.imageAdList,
this.moduleList,
this.newsList,
});
Data.fromJson(Map<String, dynamic> json) {
if (json['imageAdList'] != null) {
imageAdList = <ImageAdList>[];
json['imageAdList'].forEach((v) {
imageAdList!.add(new ImageAdList.fromJson(v));
});
}
if (json['moduleList'] != null) {
moduleList = <ModuleList>[];
json['moduleList'].forEach((v) {
moduleList!.add(new ModuleList.fromJson(v));
});
}
if (json['newsList'] != null) {
newsList = <NewsList>[];
json['newsList'].forEach((v) {
newsList!.add(new NewsList.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.imageAdList != null) {
data['imageAdList'] = this.imageAdList!.map((v) => v.toJson()).toList();
}
if (this.moduleList != null) {
data['moduleList'] = this.moduleList!.map((v) => v.toJson()).toList();
}
if (this.newsList != null) {
data['newsList'] = this.newsList!.map((v) => v.toJson()).toList();
}
return data;
}
}
class ImageAdList {
String? defaultImage;
int? id;
int? shopId;
int? status;
String? title;
ImageAdList(
{this.defaultImage, this.id, this.shopId, this.status, this.title});
ImageAdList.fromJson(Map<String, dynamic> json) {
defaultImage = json['defaultImage'];
id = json['id'];
shopId = json['shopId'];
status = json['status'];
title = json['title'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['defaultImage'] = this.defaultImage;
data['id'] = this.id;
data['shopId'] = this.shopId;
data['status'] = this.status;
data['title'] = this.title;
return data;
}
}
class ModuleList {
List<Modules>? modules;
List<Products>? products;
ModuleList({this.modules, this.products});
ModuleList.fromJson(Map<String, dynamic> json) {
if (json['modules'] != null) {
modules = <Modules>[];
json['modules'].forEach((v) {
modules!.add(new Modules.fromJson(v));
});
}
if (json['products'] != null) {
products = <Products>[];
json['products'].forEach((v) {
products!.add(new Products.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.modules != null) {
data['modules'] = this.modules!.map((v) => v.toJson()).toList();
}
if (this.products != null) {
data['products'] = this.products!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Modules {
String? adImage;
String? adLink;
int? adStatus;
int? adType;
int? adTypeId;
int? id;
int? shopId;
Modules(
{this.adImage,
this.adLink,
this.adStatus,
this.adType,
this.adTypeId,
this.id,
this.shopId});
Modules.fromJson(Map<String, dynamic> json) {
adImage = json['adImage'];
adLink = json['adLink'];
adStatus = json['adStatus'];
adType = json['adType'];
adTypeId = json['adTypeId'];
id = json['id'];
shopId = json['shopId'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['adImage'] = this.adImage;
data['adLink'] = this.adLink;
data['adStatus'] = this.adStatus;
data['adType'] = this.adType;
data['adTypeId'] = this.adTypeId;
data['id'] = this.id;
data['shopId'] = this.shopId;
return data;
}
}
class Products {
List<String>? imageList;
String? model;
int? pdId;
String? pdName;
double? pdPrice;
int? shopId;
Products({
this.imageList,
this.model,
this.pdId,
this.pdName,
this.pdPrice,
this.shopId,
});
Products.fromJson(Map<String, dynamic> json) {
imageList = json['imageList'].cast<String>();
model = json['model'];
pdId = json['pdId'];
pdName = json['pdName'];
pdPrice = json['pdPrice'];
shopId = json['shopId'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['imageList'] = this.imageList;
data['model'] = this.model;
data['pdId'] = this.pdId;
data['pdName'] = this.pdName;
data['pdPrice'] = this.pdPrice;
data['shopId'] = this.shopId;
return data;
}
}
class NewsList {
String? newsTitle;
NewsList({
this.newsTitle,
});
NewsList.fromJson(Map<String, dynamic> json) {
newsTitle = json['newsTitle'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['newsTitle'] = this.newsTitle;
return data;
}
}