所以我想取出用户名中的第一个字母,即内部用户,为每个列表view.builder单元格!
我的问题是我找不到任何好的方法来序列化其中包含对象列表的儿子,这已经成为一个真正的问题!
例如,我们将有一个包含所有项目的listview,然后显示每个单元格内的成员名称。就像在这种情况下:名称将是:“test med teacher chat”,成员将是:“hugo”和“studentone”!
This is what I am thinking but the letter being the first letter of every users username!
JSON:
[
{
"id": 81,
"users": [
{
"username": "hugo",
"fullname": "Hugo Johnsson"
},
{
"username": "studentone",
"fullname": "Student One"
}
],
"title": "test med teacher chat",
"description": "This project does not have a description.",
"subject": "No subject",
"deadline": "2019-01-06",
"days_left": "98 days ago",
"overview_requests": [
{
"id": 28,
"user": {
"username": "hugo",
"fullname": "Hugo Johnsson"
},
"group": 81
}
]
},
未来:
Future<List<Project>> _getProjects() async {
var data = await http.get(
"http://studieplaneraren.pythonanywhere.com/api/projects/${UserLog().Username}/?format=json");
var jsonData = json.decode(data.body); //an array of json objects
List<Project> allProjects = [];
for (var JData in jsonData) {
Project project = Project(
JData["id"],
JData["title"],
JData["description"],
JData["deadline"],
JData["subject"],
JData["days_left"],
JData["users"]);
allProjects.add(project);
}
return allProjects;
}
定义:
class Project {
final int id;
final String title;
final String description;
final String deadline;
final String subject;
final String days_left;
final List users;
Project(
this.id,
this.title,
this.description,
this.deadline,
this.subject,
this.days_left,
this.users
);
}
我不太确定我是否理解你在寻找什么,但是如果你想找到一种解析json的方法,那么你可以得到列表中每个用户名的第一个字母,你只需要了解结构json和你正在获得,并确保你像往常一样得到所需的领域。
我使用DartPad为您做了一个小例子,并为您的json添加了一些用户名。
const List<Map<String,dynamic>> map = [
{
"id": 81,
"users": [
{
"username": "hugo",
"fullname": "Hugo Johnsson"
},
{
"username": "studentone",
"fullname": "Student One"
},
{
"username": "anotherStudent",
"fullname": "Student Two"
},
{
"username": "oneMore",
"fullname": "Student Three"
},
{
"username": "isItEnough",
"fullname": "Student N"
}
],
"title": "test med teacher chat",
"description": "This project does not have a description.",
"subject": "No subject",
"deadline": "2019-01-06",
"days_left": "98 days ago",
"overview_requests": [
{
"id": 28,
"user": {
"username": "hugo",
"fullname": "Hugo Johnsson"
},
"group": 81
}
]
}
];
void main() {
map.forEach((element) {
final List<Map<String,dynamic>> users = element['users'] as List;
users.forEach((user){
final String firstUsernameLetter = user['username'][0];
print('First letter of ${user['username']}: $firstUsernameLetter');
});
});
}
First letter of hugo: h
First letter of studentone: s
First letter of anotherStudent: a
First letter of oneMore: o
First letter of isItEnough: i