【发布时间】:2021-08-23 15:40:46
【问题描述】:
我的应用从 REST API 获取一些帖子,这些帖子带有以下数据:{ 标题、照片、用户名... } 我想要做的不是显示 userID,而是显示 用户名。我想也许可以将 id 从 post 模型传递给 user 模型,但我不知道该怎么做,也不知道这是否是最好的方法。
用户模型
class User {
String id;
String name;
String email;
String createdAt;
User(
{
this.id,
this.name,
this.createdAt,
this.email
});
User.fromJson(Map<String, dynamic> json) {
id = json['_id'];
name = json['name'];
email = json['email'];
createdAt = json['createdAt'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['_id'] = this.id;
data['name'] = this.name;
data['email'] = this.email;
data['createdAt'] = this.createdAt;
return data;
}
}
后模型
class Post {
String user;
String id;
String title;
String content;
String address;
String category;
String price;
String photo;
Post(
{this.user,
this.title,
this.category,
this.content,
this.address,
this.price,
this.id,
this.photo});
Post.fromJson(Map<String, dynamic> json) {
id = json['_id'];
title = json['title'];
price = json['price'];
user = json['user'];
content = json['content'];
category = json['category'];
address = json['address'];
photo = json['photo'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['title'] = this.title;
data['price'] = this.price;
data['user'] = this.user;
data['content'] = this.content;
data['category'] = this.category;
data['address'] = this.address;
return data;
}
}
主屏幕我想在其中显示用户名
AutoSizeText(
reversedList[index].user,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18),
)
【问题讨论】:
-
你能添加你的restapi结果吗