Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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
Json 无法在列表视图中加载变量_Json_Dart_Flutter - Fatal编程技术网

Json 无法在列表视图中加载变量

Json 无法在列表视图中加载变量,json,dart,flutter,Json,Dart,Flutter,描述:我试图在ListView中显示json文件中的值。我创建了一个类来读取json文件并从键中获取值。这很好,但是,我无法将键值带入ListView。我使用名为“EmployeeInfo”的单独类获取键值,并使用名为“jsonContent”的方法读取json文件内容。到目前为止,我可以实例化该类,执行该方法,并将类变量打印到控制台上。但是,我无法将这个类变量值加载到ListView标题中 我无法在此代码中加载变量,但可以通过:print(jsonrd.EmployeeJson[“Name”]

描述:我试图在ListView中显示json文件中的值。我创建了一个类来读取json文件并从键中获取值。这很好,但是,我无法将键值带入ListView。我使用名为“EmployeeInfo”的单独类获取键值,并使用名为“jsonContent”的方法读取json文件内容。到目前为止,我可以实例化该类,执行该方法,并将类变量打印到控制台上。但是,我无法将这个类变量值加载到ListView标题中

我无法在此代码中加载变量,但可以通过:print(jsonrd.EmployeeJson[“Name”]

import 'package:flutter/material.dart';
import 'package:emas_app/Dependant.dart' as Dep;

import 'dart:convert';

void main() => runApp(new Fourth());



class Fourth extends StatefulWidget {

  @override
  MemberInfoState createState() => new MemberInfoState();
}
class MemberInfoState extends State<Fourth>{
  List  data;
  @override
  Widget build(BuildContext context) {
    var  jsonrd = new Dep.EmployeeInfo(); // Go to the house and open the door
    jsonrd.jsonContent(); // E

    return new Scaffold(

      body:  new Container(

        child: new Center(
          child:new FutureBuilder(
            future: DefaultAssetBundle
                .of(context)
                .loadString('assets/MemberInfo.json'),
            builder: (context,snapshot){

              var mydata = JSON.decode(snapshot.data.toString());
              print("mydata: " + mydata.toString());
              var jsondata = new Dep.EmployeeInfo();
              jsondata.jsonContent();
              final name = jsondata.EmployeeJSON["Name"].toString();

              return new ListView.builder(
                itemBuilder:(BuildContext context,int index){
                  return new Card(
                    child:new Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: <Widget>[
                        new ListTile(
                          title: new Text("Name " ),
                          subtitle: new Text(name),
                        ),
                        new ListTile(
                          title: new Text("Identification " ),
                          subtitle: new Text(  mydata[index]["Employee"]["Identification"]),
                        ),
                        new ListTile(
                          title: new Text("Company " ),
                          subtitle: new Text(  mydata[index]["Employee"]["Company"]),
                        ),
                        new ListTile(
                          title: new Text("Date Of Birth " ),
                          subtitle: new Text(  mydata[index]["Employee"]["DateOfBirth"]),
                        ),

                        const Divider(
                          color: Colors.white,
                          height: 50.0,
                        ),

                        new MaterialButton(
                            color: Colors.indigo,
                            height:50.0,
                            minWidth: 50.0,
                            textColor: Colors.white,
                            child: new Text("More"),
                            onPressed: () => print(jsonrd.EmployeeJSON["Name"])

                            ),
                      ],

                    ),

                  );





                },
                itemCount: mydata ==  null ? 0 : mydata.length,
              );


            },
          ),
//
//
        ),

      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
将“package:emas_app/Dependent.dart”作为Dep导入;
导入“dart:convert”;
void main()=>runApp(新的第四个());
第四类扩展了StatefulWidget{
@凌驾
MemberInfoState createState()=>new MemberInfoState();
}
类MemberInfoState扩展了状态{
列出数据;
@凌驾
小部件构建(构建上下文){
var jsonrd=new Dep.EmployeeInfo();//到房子里去开门
jsonrd.jsonContent();//E
归还新脚手架(
主体:新容器(
孩子:新中心(
孩子:新未来建设者(
未来:DefaultAssetBundle
.of(上下文)
.loadString('assets/MemberInfo.json'),
生成器:(上下文,快照){
var mydata=JSON.decode(snapshot.data.toString());
打印(“mydata:+mydata.toString());
var jsondata=新的副员工信息();
jsondata.jsonContent();
final name=jsondata.EmployeeJSON[“name”].toString();
返回新的ListView.builder(
itemBuilder:(构建上下文,int索引){
归还新卡(
子:新列(
crossAxisAlignment:crossAxisAlignment.stretch,
儿童:[
新ListTile(
标题:新文本(“名称”),
副标题:新文本(名称),
),
新ListTile(
标题:新文本(“标识”),
副标题:新文本(我的数据[索引][“员工”][“身份]),
),
新ListTile(
标题:新文本(“公司”),
字幕:新文本(mydata[索引][“员工”][“公司”]),
),
新ListTile(
标题:新文本(“出生日期”),
字幕:新文本(mydata[索引][“员工”][“出生日期”]),
),
常数分频器(
颜色:颜色,白色,
身高:50.0,
),
新材料按钮(
颜色:颜色,靛蓝,
身高:50.0,
最小宽度:50.0,
textColor:Colors.white,
儿童:新文本(“更多”),
onPressed:()=>print(jsonrd.EmployeeJSON[“Name”])
),
],
),
);
},
itemCount:mydata==null?0:mydata.length,
);
},
),
//
//
),
),
);
}
}
调用Json(代码):

进口“包装:颤振/材料.省道”

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;


final String url = "http://crm.emastpa.com.my/MemberInfo.json";
List depData;



class EmployeeInfo {
//  String jsonContent;
  List data;
  String Employee;
  String empname;
  String empdep;
  Map <String, dynamic> EmployeeJSON = new Map<String, dynamic>();

  void jsonContent() {

    Future<String> getData() async {
      var res = await http.get(
          Uri.encodeFull("http://crm.emastpa.com.my/MemberInfo.json"),
          headers: {"Accept": "application/json"});

      var resBody = json.decode(res.body);
      this.data = resBody;

      this.EmployeeJSON = resBody[0]["Employee"];
      Employee = resBody[0]["Employee"]["Name"];
      depData = resBody[0]["Dependents"];

      this.empname = this.EmployeeJSON["Name"];
      this.empdep= depData[0]["Dependents"];
      return "Success!";
    }
    getData();
  }

}
导入'dart:async';
导入“dart:convert”;
将“package:http/http.dart”导入为http;
最终字符串url=”http://crm.emastpa.com.my/MemberInfo.json";
列出数据;
类雇员信息{
//字符串jsonContent;
列出数据;
字符串雇员;
字符串名称;
字符串empdep;
Map EmployeeJSON=new Map();
void jsonContent(){
Future getData()异步{
var res=wait http.get(
Uri.encodeFull(“http://crm.emastpa.com.my/MemberInfo.json"),
标题:{“接受”:“应用程序/json”});
var resBody=json.decode(res.body);
this.data=resBody;
this.EmployeeJSON=resBody[0][“Employee”];
Employee=resBody[0][“Employee”][“Name”];
depData=resBody[0][“依赖项”];
this.empname=this.EmployeeJSON[“Name”];
this.empdep=depData[0][“Dependents”];
返回“成功!”;
}
getData();
}
}
输出 正在执行热重新加载。。。 I/颤振(24432):mydata:null 在1696ms内重新加载549个库中的8个。 I/颤振(24432):mydata:null I/颤振(24432):mydata:null I/颤振(24432):mydata:null I/颤振(24432):mydata:null
与设备的连接中断。

哇,你的代码真的很难编写。你正在构建方法中执行future,而不仅仅是在FutureBuilder中期望它的结果

import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:async';
import 'package:http/http.dart' as http;

final String url = "http://crm.emastpa.com.my/MemberInfo.json";

class EmployeeInfo {
  String Employee;
  String empname;
  String empdep;
  Map<String, dynamic> EmployeeJSON = new Map<String, dynamic>();
}

Future<String> jsonContent() async {
  var res = await http.get(
      Uri.encodeFull("http://crm.emastpa.com.my/MemberInfo.json"),
      headers: {"Accept": "application/json"});
  return res.body;
}

void main() => runApp(new Fourth());

class Fourth extends StatefulWidget {
  @override
  MemberInfoState createState() => new MemberInfoState();
}

class MemberInfoState extends State<Fourth> {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: new Scaffold(
        body: new Container(
          child: new Center(
            child: new FutureBuilder<String>(
              future: jsonContent(),
              builder: (context, snapshot) {
                if (snapshot?.hasData) {
                  var mydata = json.decode(snapshot.data);
                  final name = mydata[0]["Employee"]["Name"];

                  return new ListView.builder(
                    itemBuilder: (BuildContext context, int index) {
                      return new Card(
                        child: new Column(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: <Widget>[
                            new ListTile(
                              title: new Text("Name "),
                              subtitle: new Text(name),
                            ),
                            new ListTile(
                              title: new Text("Identification "),
                              subtitle: new Text(
                                  mydata[index]["Employee"]["Identification"]),
                            ),
                            new ListTile(
                              title: new Text("Company "),
                              subtitle: new Text(
                                  mydata[index]["Employee"]["Company"]),
                            ),
                            new ListTile(
                              title: new Text("Date Of Birth "),
                              subtitle: new Text(
                                  mydata[index]["Employee"]["DateOfBirth"]),
                            ),
                            const Divider(
                              color: Colors.white,
                              height: 50.0,
                            ),
                            new MaterialButton(
                              color: Colors.indigo,
                              height: 50.0,
                              minWidth: 50.0,
                              textColor: Colors.white,
                              child: new Text("More"),
                            ),
                          ],
                        ),
                      );
                    },
                    itemCount: mydata == null ? 0 : mydata.length,
                  );
                } else {
                  return CircularProgressIndicator();
                }
              },
            ),
          ),
        ),
      ),
    );
  }
}
我已经做了深刻的修改,仍然保持着你想做的事情的精神

但是您应该真正创建一个返回员工列表的Future,而不是在FutureBuilder中处理html响应

import 'package:flutter/material.dart';
import 'dart:convert';
import 'dart:async';
import 'package:http/http.dart' as http;

final String url = "http://crm.emastpa.com.my/MemberInfo.json";

class EmployeeInfo {
  String Employee;
  String empname;
  String empdep;
  Map<String, dynamic> EmployeeJSON = new Map<String, dynamic>();
}

Future<String> jsonContent() async {
  var res = await http.get(
      Uri.encodeFull("http://crm.emastpa.com.my/MemberInfo.json"),
      headers: {"Accept": "application/json"});
  return res.body;
}

void main() => runApp(new Fourth());

class Fourth extends StatefulWidget {
  @override
  MemberInfoState createState() => new MemberInfoState();
}

class MemberInfoState extends State<Fourth> {

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: new Scaffold(
        body: new Container(
          child: new Center(
            child: new FutureBuilder<String>(
              future: jsonContent(),
              builder: (context, snapshot) {
                if (snapshot?.hasData) {
                  var mydata = json.decode(snapshot.data);
                  final name = mydata[0]["Employee"]["Name"];

                  return new ListView.builder(
                    itemBuilder: (BuildContext context, int index) {
                      return new Card(
                        child: new Column(
                          crossAxisAlignment: CrossAxisAlignment.stretch,
                          children: <Widget>[
                            new ListTile(
                              title: new Text("Name "),
                              subtitle: new Text(name),
                            ),
                            new ListTile(
                              title: new Text("Identification "),
                              subtitle: new Text(
                                  mydata[index]["Employee"]["Identification"]),
                            ),
                            new ListTile(
                              title: new Text("Company "),
                              subtitle: new Text(
                                  mydata[index]["Employee"]["Company"]),
                            ),
                            new ListTile(
                              title: new Text("Date Of Birth "),
                              subtitle: new Text(
                                  mydata[index]["Employee"]["DateOfBirth"]),
                            ),
                            const Divider(
                              color: Colors.white,
                              height: 50.0,
                            ),
                            new MaterialButton(
                              color: Colors.indigo,
                              height: 50.0,
                              minWidth: 50.0,
                              textColor: Colors.white,
                              child: new Text("More"),
                            ),
                          ],
                        ),
                      );
                    },
                    itemCount: mydata == null ? 0 : mydata.length,
                  );
                } else {
                  return CircularProgressIndicator();
                }
              },
            ),
          ),
        ),
      ),
    );
  }
}
导入“包装:颤振/材料.省道”;
导入“dart:convert”;
导入“dart:async”;
将“package:http/http.dart”导入为http;
最终字符串url=”http://crm.emastpa.com.my/MemberInfo.json";
类雇员信息{
字符串雇员;
字符串名称;
字符串empdep;
Map EmployeeJSON=new Map();
}
Future jsonContent()异步{
var res=wait http.get(
Uri.encodeFull(“http://crm.emastpa.com.my/MemberInfo.json"),
标题:{“接受”:“应用程序/json”});
返回实体;
}
void main()=>runApp(新的第四个());
第四类扩展了StatefulWidget{
@凌驾
MemberInfoState createState()=>new MemberInfoState();
}
类MemberInfoState扩展了状态{
@凌驾
小部件构建(BuildContext)