1

So I want to take out the first letter in the username, that's inside users, for every single list view.builder cell!

My problem is that I can't find any good way to serialize son that has a list of objects inside it and it has become a real problem!

For example we will have a listview with all the projects like this one, then display the names of the members inside every cell. Like in this case: The name will be: "test med teacher chat", and the members will be: "hugo", and "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:

  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;

}

DEFINING:

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
  );
}
2
  • The names will be displayed with their first letter in Circle Avatars. Like this: O - O - O, and the letter that will be displayed in them is the first letter of the users in the project! Commented Apr 14, 2019 at 19:25
  • I am not understandin g it well. where are you stuck? Commented Apr 14, 2019 at 19:31

1 Answer 1

1

I'm not quite sure if I understood what you are looking for, but if you are trying to find a way to parse that json so you can get always the first letter for each username in the list, you just need to understand the structure of the json and that you are getting and make sure that you get the desired field as you usually would do.

I made you a little example using the DartPad and also added a few more usernames to your json.

The data

   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
            }
       ]

            }
        ];

Getting the data

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');
    });
  });


}

Output

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.