简体   繁体   中英

Flutter Json with array of strings into Futurebuilder

So I have a JSON with an array of Strings. Can someone please tell me how to display the username of every object in a future builder with a listview.builder.

I have looked everywhere and can't seem to find any good solutions.

This is the JSON Tree:

{
    "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
        }
    ]
},

This is the Project Class:

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

What I have tried: I don't know how to make this loop for the "users" to be able to get the names out.

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

And then abviously running that through a futurebuilder in the ui

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

You can get users like this -

var jsonUsers = JDate["users"];

and then iterate over them like this -

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

From your FutureBuilder , I see you have a list users . I have just created objects of USER and added them to the list. Does this help you?

//THE FUTURE Future> _getUSERS() 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


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

}

}

I also tried this, why won't it work?

  //THE FUTURE

Future> _getUSERS() 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<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;

}

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