我正在尝试连接到WordPress与Dio 4和Flutter
我在存储库中发现错误
错误:_TypeError(类型'_InternalLinkedHashMap〈字符串,动态〉'不是类型'String'的子类型)
存储库
Future<List<PostModel>> getPostsRequested() async {
try {
final response = await postApi.getPostsApi();
final posts = ( (response.data) as List)
// ERROR HERE
.map((e) => PostModel.fromJson(e))
.toList();
return posts;
} on DioError catch (e) {
final errorMessage = DioExceptions.fromDioError(e).toString();
throw errorMessage;
}
}
阿皮
Future<Response> getPostsApi() async {
try {
final Response response = await dioClient.get(Endpoints.posts);
return response;
} catch (e) {
rethrow;
}
}
型号
import 'dart:convert';
class PostModel {
PostModel({
this.id,
this.date,
this.dateGmt,
this.guid,
this.modified,
this.modifiedGmt,
this.slug,
this.status,
this.type,
this.link,
this.title,
this.content,
this.excerpt,
this.author,
this.featuredMedia,
this.commentStatus,
this.pingStatus,
this.sticky,
this.template,
this.format,
this.meta,
this.categories,
this.tags,
this.links,
});
int? id;
DateTime? date;
DateTime? dateGmt;
Guid? guid;
DateTime? modified;
DateTime? modifiedGmt;
String? slug;
String? status;
String? type;
String? link;
Guid? title;
Content? content;
Content? excerpt;
int? author;
int? featuredMedia;
String? commentStatus;
String? pingStatus;
bool? sticky;
String? template;
String? format;
List<dynamic>? meta;
List<int>? categories;
List<dynamic>? tags;
Links? links;
PostModel copyWith({
int? id,
DateTime? date,
DateTime? dateGmt,
Guid? guid,
DateTime? modified,
DateTime? modifiedGmt,
String? slug,
String? status,
String? type,
String? link,
Guid? title,
Content? content,
Content? excerpt,
int? author,
int? featuredMedia,
String? commentStatus,
String? pingStatus,
bool? sticky,
String? template,
String? format,
List<dynamic>? meta,
List<int>? categories,
List<dynamic>? tags,
Links? links,
}) => PostModel(
id: id ?? this.id,
date: date ?? this.date,
dateGmt: dateGmt ?? this.dateGmt,
guid: guid ?? this.guid,
modified: modified ?? this.modified,
modifiedGmt: modifiedGmt ?? this.modifiedGmt,
slug: slug ?? this.slug,
status: status ?? this.status,
type: type ?? this.type,
link: link ?? this.link,
title: title ?? this.title,
content: content ?? this.content,
excerpt: excerpt ?? this.excerpt,
author: author ?? this.author,
featuredMedia: featuredMedia ?? this.featuredMedia,
commentStatus: commentStatus ?? this.commentStatus,
pingStatus: pingStatus ?? this.pingStatus,
sticky: sticky ?? this.sticky,
template: template ?? this.template,
format: format ?? this.format,
meta: meta ?? this.meta,
categories: categories ?? this.categories,
tags: tags ?? this.tags,
links: links ?? this.links,
);
factory PostModel.fromJson(String str) => PostModel.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory PostModel.fromMap(Map<String, dynamic> json) => PostModel(
id: json["id"],
date: DateTime.parse(json["date"]),
dateGmt: DateTime.parse(json["date_gmt"]),
guid: Guid.fromMap(json["guid"]),
modified: DateTime.parse(json["modified"]),
modifiedGmt: DateTime.parse(json["modified_gmt"]),
slug: json["slug"],
status: json["status"],
type: json["type"],
link: json["link"],
title: Guid.fromMap(json["title"]),
content: Content.fromMap(json["content"]),
excerpt: Content.fromMap(json["excerpt"]),
author: json["author"],
featuredMedia: json["featured_media"],
commentStatus: json["comment_status"],
pingStatus: json["ping_status"],
sticky: json["sticky"],
template: json["template"],
format: json["format"],
meta: List<dynamic>.from(json["meta"].map((x) => x)),
categories: List<int>.from(json["categories"].map((x) => x)),
tags: List<dynamic>.from(json["tags"].map((x) => x)),
links: Links.fromMap(json["_links"]),
);
Map<String, dynamic> toMap() => {
"id": id,
"date": date?.toIso8601String(),
"date_gmt": dateGmt?.toIso8601String(),
"guid": guid?.toMap(),
"modified": modified?.toIso8601String(),
"modified_gmt": modifiedGmt?.toIso8601String(),
"slug": slug,
"status": status,
"type": type,
"link": link,
"title": title?.toMap(),
"content": content?.toMap(),
"excerpt": excerpt?.toMap(),
"author": author,
"featured_media": featuredMedia,
"comment_status": commentStatus,
"ping_status": pingStatus,
"sticky": sticky,
"template": template,
"format": format,
"meta": List<dynamic>.from(meta!.map((x) => x)),
"categories": List<dynamic>.from(categories!.map((x) => x)),
"tags": List<dynamic>.from(tags!.map((x) => x)),
"_links": links!.toMap(),
};
}
class Content {
Content({
this.rendered,
this.protected,
});
String? rendered;
bool? protected;
Content copyWith({
String? rendered,
bool? protected,
}) =>
Content(
rendered: rendered ?? this.rendered,
protected: protected ?? this.protected,
);
factory Content.fromJson(String str) => Content.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Content.fromMap(Map<String, dynamic> json) => Content(
rendered: json["rendered"],
protected: json["protected"],
);
Map<String, dynamic> toMap() => {
"rendered": rendered,
"protected": protected,
};
}
class Guid {
Guid({
this.rendered,
});
String? rendered;
Guid copyWith({
String? rendered,
}) => Guid(
rendered: rendered ?? this.rendered,
);
factory Guid.fromJson(String str) => Guid.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Guid.fromMap(Map<String, dynamic> json) => Guid(
rendered: json["rendered"],
);
Map<String, dynamic> toMap() => {
"rendered": rendered,
};
}
class Links {
Links({
this.self,
this.collection,
this.about,
this.author,
this.replies,
this.versionHistory,
this.wpAttachment,
this.wpTerm,
this.curies,
});
List<About>? self;
List<About>? collection;
List<About>? about;
List<Author>? author;
List<Author>? replies;
List<VersionHistory>? versionHistory;
List<About>? wpAttachment;
List<WpTerm>? wpTerm;
List<Cury>? curies;
Links copyWith({
List<About>? self,
List<About>? collection,
List<About>? about,
List<Author>? author,
List<Author>? replies,
List<VersionHistory>? versionHistory,
List<About>? wpAttachment,
List<WpTerm>? wpTerm,
List<Cury>? curies,
}) =>
Links(
self: self ?? this.self,
collection: collection ?? this.collection,
about: about ?? this.about,
author: author ?? this.author,
replies: replies ?? this.replies,
versionHistory: versionHistory ?? this.versionHistory,
wpAttachment: wpAttachment ?? this.wpAttachment,
wpTerm: wpTerm ?? this.wpTerm,
curies: curies ?? this.curies,
);
factory Links.fromJson(String str) => Links.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Links.fromMap(Map<String, dynamic> json) => Links(
self: List<About>.from(json["self"].map((x) => About.fromMap(x))),
collection: List<About>.from(json["collection"].map((x) => About.fromMap(x))),
about: List<About>.from(json["about"].map((x) => About.fromMap(x))),
author: List<Author>.from(json["author"].map((x) => Author.fromMap(x))),
replies: List<Author>.from(json["replies"].map((x) => Author.fromMap(x))),
versionHistory: List<VersionHistory>.from(json["version-history"].map((x) => VersionHistory.fromMap(x))),
wpAttachment: List<About>.from(json["wp:attachment"].map((x) => About.fromMap(x))),
wpTerm: List<WpTerm>.from(json["wp:term"].map((x) => WpTerm.fromMap(x))),
curies: List<Cury>.from(json["curies"].map((x) => Cury.fromMap(x))),
);
Map<String, dynamic> toMap() => {
"self": List<dynamic>.from(self!.map((x) => x.toMap())),
"collection": List<dynamic>.from(collection!.map((x) => x.toMap())),
"about": List<dynamic>.from(about!.map((x) => x.toMap())),
"author": List<dynamic>.from(author!.map((x) => x.toMap())),
"replies": List<dynamic>.from(replies!.map((x) => x.toMap())),
"version-history": List<dynamic>.from(versionHistory!.map((x) => x.toMap())),
"wp:attachment": List<dynamic>.from(wpAttachment!.map((x) => x.toMap())),
"wp:term": List<dynamic>.from(wpTerm!.map((x) => x.toMap())),
"curies": List<dynamic>.from(curies!.map((x) => x.toMap())),
};
}
class About {
About({
this.href,
});
String? href;
About copyWith({
String? href,
}) =>
About(
href: href ?? this.href,
);
factory About.fromJson(String str) => About.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory About.fromMap(Map<String, dynamic> json) => About(
href: json["href"],
);
Map<String, dynamic> toMap() => {
"href": href,
};
}
class Author {
Author({
this.embeddable,
this.href,
});
bool? embeddable;
String? href;
Author copyWith({
bool? embeddable,
String? href,
}) =>
Author(
embeddable: embeddable ?? this.embeddable,
href: href ?? this.href,
);
factory Author.fromJson(String str) => Author.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Author.fromMap(Map<String, dynamic> json) => Author(
embeddable: json["embeddable"],
href: json["href"],
);
Map<String, dynamic> toMap() => {
"embeddable": embeddable,
"href": href,
};
}
class Cury {
Cury({
this.name,
this.href,
this.templated,
});
String? name;
String? href;
bool? templated;
Cury copyWith({
String? name,
String? href,
bool? templated,
}) =>
Cury(
name: name ?? this.name,
href: href ?? this.href,
templated: templated ?? this.templated,
);
factory Cury.fromJson(String str) => Cury.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Cury.fromMap(Map<String, dynamic> json) => Cury(
name: json["name"],
href: json["href"],
templated: json["templated"],
);
Map<String, dynamic> toMap() => {
"name": name,
"href": href,
"templated": templated,
};
}
class VersionHistory {
VersionHistory({
this.count,
this.href,
});
int? count;
String? href;
VersionHistory copyWith({
int? count,
String? href,
}) =>
VersionHistory(
count: count ?? this.count,
href: href ?? this.href,
);
factory VersionHistory.fromJson(String str) => VersionHistory.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory VersionHistory.fromMap(Map<String, dynamic> json) => VersionHistory(
count: json["count"],
href: json["href"],
);
Map<String, dynamic> toMap() => {
"count": count,
"href": href,
};
}
class WpTerm {
WpTerm({
this.taxonomy,
this.embeddable,
this.href,
});
String? taxonomy;
bool? embeddable;
String? href;
WpTerm copyWith({
String? taxonomy,
bool? embeddable,
String? href,
}) => WpTerm(
taxonomy: taxonomy ?? this.taxonomy,
embeddable: embeddable ?? this.embeddable,
href: href ?? this.href,
);
factory WpTerm.fromJson(String str) => WpTerm.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory WpTerm.fromMap(Map<String, dynamic> json) => WpTerm(
taxonomy: json["taxonomy"],
embeddable: json["embeddable"],
href: json["href"],
);
Map<String, dynamic> toMap() => {
"taxonomy": taxonomy,
"embeddable": embeddable,
"href": href,
};
}
2条答案
按热度按时间p1iqtdky1#
你的
response.data
是List<Map>
,所以当你试图Map它时,它会返回一个Map
,而不是string
。所以当你试图用fromJson
解析它时,它会给你这个错误,你应该使用fromMap
。更改此
至:
lx0bsm1f2#
如果json文件以“{”开始,请尝试以下操作
并且变更了
至
否则,如果以“[”开始,请尝试