简体   繁体   English

如何创建Flutter Futurebuilder函数,显示从JSON中获取的字符串数组?

[英]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. 我在Flutter中有一个项目管理应用程序,并将这些项目排列在列表视图中。 I take the data from an API in JSON and want to display the users that are working on the project! 我从JSON中的API获取数据,并希望显示正在处理项目的用户!

JSON: 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: 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): 未来(在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. UI存在一个返回listview.builder的futurebuilder。 The future is the function above and I want to display usernames inside another listview inside the other listview. 未来是上面的函数,我想在另一个listview中的另一个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 所以这就是我想要它的样子,区别在于Circle Avatars中的Text应该是用户名的第一个字母

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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在另一个FutureBuilder中展开FutureBuilder,取自字符串的JSON数组 - Flutter FutureBuilder inside another FutureBuilder, Taken From JSON array of Strings 将带有字符串数组的Json打入Futurebuilder - Flutter Json with array of strings into Futurebuilder Flutter:在Futurebuilder中显示包含Listview的JSON字符串数组 - Flutter: Display an array of strings from JSON in Futurebuilder containing a Listview 如何解决从 FutureBuilder 中的本地主机收到的 JSON object - How do I resolve a JSON object received from localhost in FutureBuilder 我如何使用 Flutter 中从 json 解析的嵌套映射列表中的函数创建对象 - How do i create an object using a function from a list of nested maps parse from json in Flutter Delphi-如何从字符串数组创建TJSONArray? - Delphi - How do I create a TJSONArray from a array of strings? 如何从 swift 中的字典创建字符串数组? - how do I create array of strings from dictionary in swift? 如何在不进行双重编码的情况下从json字符串数组创建json字符串? - How can I create a json string from an array of json strings without double encoding? 如何从json数组创建List集合? - How do i create a List collection from a json array? 如何从 JavaScript 中的数组创建 JSON 文件? - How do i create a JSON file from an array in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM