如何创建Flutter Futurebuilder函数以显示从JSON提取的字符串数组?

时间:2019-04-23 13:05:41

标签: json api listview flutter future

我在Flutter中有一个项目管理应用程序,并在列表视图中排列了这些项目。我从JSON中的API中获取数据,并希望显示正在处理该项目的用户!

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

}

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

未来(在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;
 }

UI包含一个futurebuilder,它返回一个listview.builder。将来是上面的功能,我想在另一个列表视图中的另一个列表视图中显示用户名。

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

我如何取出字符串?

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

1 个答案:

答案 0 :(得分:0)

小部件

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]),
      ),
    ),
  );
}

服务

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;

}