【问题标题】:Accessing data in a nested instance object in Flutter(Dart)在 Flutter(Dart) 中访问嵌套实例对象中的数据
【发布时间】:2021-10-01 14:36:54
【问题描述】:

我对 Flutter 比较陌生。我正在使用异步未来和未来构建来使用嵌套对象中的返回数据填充小部件。我遇到的问题是访问对象内的嵌套对象列表。当我登录 snapshot.data!.license 而不是 json? 时,我不断收到 [Instance of 'License', Instance of 'License', Instance of 'License', Instance of 'License', Instance of 'License'],当我尝试创建列表视图时,输出为空。 我的观点是这样的:

body: SingleChildScrollView(
            child: new FutureBuilder<Profile>(
                future: _license,
                builder: (context, snapshot) {
                  if (snapshot.hasData == false || snapshot.data == null) {
                    return CircularProgressIndicator();
                  } else if (snapshot.hasError) {
                    return Text("${snapshot.error}");
                  } else {
                    var x = snapshot.data!.license;

                    x.map((e) {
                      print(e);
                      return Row(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  Text(e.fromDate.toString()),
                                ])
                          ]);
                    });
                  }
                  return const Center(child: CircularProgressIndicator());
                })

        )

我的对象看起来像这样:

{
"id": "1",
"license": [
        {
            "license_no": "229451",
            "from_date": "2020-11-12",
            "to_date": "2021-11-30",
            "workStation": "Johpas Clinic"
        },
        {
            "license_no": "189024",
            "from_date": "2019-11-04",
            "to_date": "2020-11-30",
            "workStation": null
        },
        {
            "license_no": "109872",
            "from_date": "2016-11-30",
            "to_date": "2019-11-30",
            "workStation": null
        },
        {
            "license_no": "066356",
            "from_date": "2013-11-30",
            "to_date": "2016-11-30",
            "workStation": null
        },
        {
            "license_no": "007853    ",
            "from_date": "2010-11-30",
            "to_date": "2013-11-30",
            "workStation": null
        }
    ]
}

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    您可以在License 类中创建一个toJson 方法,然后将其用于记录目的,或者覆盖添加到dart 中创建的每个类的toString 方法。

    class License {
    String id;
    License(this.id);
    
    Map<String, dynamic> toJson() => {
     'id' : id,
    };
    

    【讨论】:

      【解决方案2】:

      我使用索引来访问数据

       } else if(snapshot.hasData) {
                      Profile? data = snapshot.data;
      
                      return ListView.builder(
                          itemCount: data!.length,
                          shrinkWrap: true,
                          primary: false,
                          itemBuilder: (BuildContext context, int index) {
                            return Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                16.height,
                                  Text(("Education History").validate(), style: boldTextStyle()),
                                16.height,
                                // child: Text(data.license[index].fromDate.toString()),
                                Row(
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    16.width,
                                    Column(
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        Text((data.registration[index].cadre).toString(),
                                            style: boldTextStyle(
                                                size: 14, color: blackColor)),
                                        8.height,
                                        Text(
                                            (data.registration[index].cadreText.toString())
                                                .validate(),
                                            style: boldTextStyle(
                                                size: 13, color: Colors.green)),
                                        8.height,
                                        Text(
                                            (data.registration[index].regNo
                                                    .toString())
                                                .validate(),
                                            style: boldTextStyle(
                                                size: 12, color: mlColorRed)),
                                      ],
                                    ).expand()
                                  ],
                                ).paddingBottom(16.0),
                              ],
                            ).paddingAll(16.0);
                          }
                      );
                      
                    }
      
      

      【讨论】:

        猜你喜欢
        • 2020-08-15
        • 2021-04-28
        • 2020-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-24
        • 2014-10-16
        • 1970-01-01
        相关资源
        最近更新 更多