Flutter Json将字符串数组放入Futurebuilder

时间:2019-04-15 12:32:07

标签: json listview flutter

所以我有一个带有字符串数组的JSON。有人可以告诉我如何在带有listview.builder的将来的构建器中显示每个对象的用户名。

我到处都是,似乎找不到任何好的解决方案。

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

这是项目类:

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

我尝试过的方法:我不知道该如何使“用户”循环出这些名称。

      Future<List<Project>> _getProjects() async {
  var data = await http.get(TheAdress);
    var jsonData = json.decode(data.body);

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

然后显然通过ui中的futurebuilder运行它

FutureBuilder(
      future: _getProjects(),
      builder: (BuildContext context, AsyncSnapshot snapshot ) {
        if (snapshot.data == null){
          return Container(
            child: Center(
              child: CircularProgressIndicator(),
            ),
          );
        }
        else return ListView.builder(
          itemCount: snapshot.data.length,
          itemBuilder: (BuildContext context, int index) {
            return ListTile(
              title: Text(snapshot.data[index].users[index].username),
            );
          }
        );
      }
    ),

3 个答案:

答案 0 :(得分:0)

您可以获得这样的用户-

var jsonUsers = JDate["users"];

然后像这样遍历它们-

for(int i = 0; i<users.length ; i++) {
  var user = jsonUsers[i];
  users.add(USER(user["username"], user["fullname"]));
}

从您的FutureBuilder中,我看到您有一个列表users。我刚刚创建了USER对象,并将它们添加到列表中。这对您有帮助吗?

答案 1 :(得分:0)

//未来   未来> _getUSERS()异步{

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


var userStrings = List;

for (var JData in jsonData) {

  var jsonUsers = JData["users"];

  for(int i = 0; i<jsonUsers.length ; i++) {
    var user = jsonUsers[i];
    jsonUsers.add(USER(user["username"], user["fullname"]));
  }

}

}

答案 2 :(得分:0)

我也尝试过,为什么不起作用?

  //THE FUTURE

未来> _getUSERS()异步{

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<USER> users;

for (var JData in jsonData) {

  var jsonUsers = JData["users"];

  for(int i = 0; i<users.length ; i++) {
    var user = jsonUsers[i];
    users.add(USER(user["username"], user["fullname"]));
  }
}


return users;

}