【问题标题】:Unable to load variable in list view无法在列表视图中加载变量
【发布时间】: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):我的数据:空 与设备的连接断开。

【问题讨论】:

    标签: json dart flutter


    【解决方案1】:

    哇,你的代码真的很难。您在 build 方法中执行了一个未来,而不仅仅是在 FutureBuilder 中期待它的结果。

    我做了一个深刻的修改,仍然保持你想做的精神。

    但您确实应该创建一个返回员工列表的 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();
                    }
                  },
                ),
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多