Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Flatter中的to list view.builder中检索json数据元素?_Json_Flutter_Flutter Listview - Fatal编程技术网

如何在Flatter中的to list view.builder中检索json数据元素?

如何在Flatter中的to list view.builder中检索json数据元素?,json,flutter,flutter-listview,Json,Flutter,Flutter Listview,我正在使用下面的代码从Flatter应用程序中的mysql服务器获取json数据元素。其中我成功地获取了数据 class CompanyDetail { String error; List<Content> content; CompanyDetail({this.error, this.content}); CompanyDetail.fromJson(Map<String, dynamic> json) { error = json['e

我正在使用下面的代码从Flatter应用程序中的mysql服务器获取json数据元素。其中我成功地获取了数据

class CompanyDetail {
  String error;
  List<Content> content;

  CompanyDetail({this.error, this.content});

  CompanyDetail.fromJson(Map<String, dynamic> json) {
    error = json['error'];
    if (json['content'] != null) {
      content = new List<Content>();
      json['content'].forEach((v) {
        content.add(new Content.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['error'] = this.error;
    if (this.content != null) {
      data['content'] = this.content.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Content {
  String id;
  String name;

  Content({this.id, this.name});

  Content.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['name'] = this.name;
    return data;
  }
}
现在我需要通过flutter中的这个列表视图生成器访问json元素,但我不知道如何访问这些元素

companyDetail.content
这是要获取并构建列表的JSON数据

JSON数据

{"error":"false",
 "content":[
{
"id":"22","name":"Johnny",},

{"id":"23","name":"Maria",},
]
}

请指导我如何获取JSON的基本数据并将其放入Listview.builder的ListTile?

Access companyDetail[“content”]

希望这有助于此代码将有助于从JSON结构中访问内容中的数据并将其映射到变量

var jsonDecode = json.decode(jsonFile);
//Decode your json file 

//then map the content details to a variable.

var content = jsonDecode['content'];
// Since data inside your json eg above is a list.
// access the first element of the list and map to a var. 
var firstdata= content[0];
// then map the id and name to a variable.
String id=firstdata['id'];
String name = firstdata['name'];

在文本小部件中的列表磁贴中使用此选项

您的JSON结构有错误
var jsonDecode = json.decode(jsonFile);
//Decode your json file 

//then map the content details to a variable.

var content = jsonDecode['content'];
// Since data inside your json eg above is a list.
// access the first element of the list and map to a var. 
var firstdata= content[0];
// then map the id and name to a variable.
String id=firstdata['id'];
String name = firstdata['name'];