【发布时间】:2018-08-13 08:58:33
【问题描述】:
描述:我试图在 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,
);
},
),
//
//
),
),
);
}
}
调用Json(代码):
导入'package:flutter/material.dart';
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();
}
}
输出 正在执行热重载... 我/颤振(24432):我的数据:空 在 1,696 毫秒内重新加载了 549 个库中的 8 个。 我/颤振(24432):我的数据:空 我/颤振(24432):我的数据:空 我/颤振(24432):我的数据:空 我/颤振(24432):我的数据:空 与设备的连接断开。
【问题讨论】: