简体   繁体   中英

How do I create a Flutter Futurebuilder function that displays an array of Strings taken from JSON?

I have a project management App in Flutter and have these projects lined up in a listview. I take the data from an API in JSON and want to display the users that are working on the project!

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": "107 days ago",
"overview_requests": [
    {
        "id": 28,
        "user": {
            "username": "hugo",
            "fullname": "Hugo Johnsson"
        },
        "group": 81
    }
]

}

The Classes inside Flutter:

class Project {
  final int id;
  final String title;
  final String description;
  final String deadline;
  final String subject;
  final String days_left;
  final List<USER> users;

  Project(
      this.id,
      this.title,
      this.description,
      this.deadline,
      this.subject,
      this.days_left,
      this.users
  );
}

class USER {
  final String username;
  final String fullname;

USER(
  this.username,
  this.fullname
  );
}

The Future (Used In Futurebuilder):

   Future<List<Project>> _getProjects() async {
   var data = await http.get(--ADRESS--);
   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[USER("username", "fullname")]

     allProjects.add(project);
   }

   return allProjects;
 }

THE UI exists of a futurebuilder that returns a listview.builder. The future is the function above and I want to display usernames inside another listview inside the other listview.

So this is what I want it to look like, the difference is that the Text inside the Circle Avatars should be the fist letters of the usernames

How I take out the string?

Text(snapshot.data[index].users.username

Widget

Widget build(BuildContext context) {
  return FutureBuilder(
    future: _getProjects(),
    builder: (context, snapshot) => ListView.builder(
      itemCount: snapshot.data.length,
      itemBuilder: (context, index) => ListView.builder(
        itemCount: snapshot.data[index].users.length,
        itemBuilder: (context, userIndex) => Text(snapshot.data[index].users[userIndex].username[0]),
      ),
    ),
  );
}

Service

Future<List<Project>> _getProjects() async {
  var data = await http.get(--ADRESS--);
  var jsonData = json.decode(data.body); //an array of json objects

  List<Project> allProjects = [];

  for (var JData in jsonData) {

    List<USER> users = JData["users"] == null 
      ? [] 
      : JData["users"]
          .map( (userJson) => USER("username", "fullname") ) // new code

    Project project = Project(
      JData["id"],
      JData["title"],
      JData["description"],
      JData["deadline"],
      JData["subject"],
      JData["days_left"],
      users,
    );

    allProjects.add(project);
 }

 return allProjects;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM