简体   繁体   English

如何访问 snapshot.data[index] 中的值。 颤振/飞镖

[英]How do I access value in snapshot.data[index] . Flutter/Dart

This is the error that I am getting:这是我得到的错误:

E/flutter (13278): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: NoSuchMethodError: Class 'String' has no instance getter 'id'.
E/flutter (13278): Receiver: "Code : LE-0000000002\nDescription : test_01\nOrganisation Unit : 01_01_04_01_SA - Shah Alam\nDate Reported : 18/09/2020\nStatus : LE110 - Pending Approval\nRunHyperlink : {classid: 25510, id: 2, mad_key: 32835}\n"
E/flutter (13278): Tried calling: id

I am trying to access id in this string.我正在尝试访问此字符串中的 id。

this is the code that I am storing in the cache:这是我存储在缓存中的代码:

List<Map<String, dynamic>> incidentList = [
                    for (final json in listNode.map((x) => x.toJson()))
                      {
                        'Code': json['field'][0]['field_value'],
                        'Description': json['field'][1]['field_value'],
                        'Organisation Unit': json['field'][2]['field_value'],
                        'Date Reported': json['field'][3]['field_value'],
                        'Status': json['field'][4]['field_value'],
                        'RunHyperlink' : json['run_hyperlink']
                      }
                  ];


                  final List<String> values =  [];
                  for(final item in incidentList){
                    String groupedElement = "";
                    for(var innerItem in item.entries)
                    {
                      groupedElement += "${innerItem.key} : ${innerItem.value}\n";
                    }
                    values.add(groupedElement);
                  }

                  await WriteCache.setListString(key: 'cache4', value: values);

and it proceeds to feed in to this code in a different page:它继续在不同的页面中输入此代码:

FutureBuilder(
              future: ReadCache.getStringList(key: 'cache4'),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return ListView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder: (context, index) {
                        return Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: ListTile(
                            onTap: () async{

                              var hyperlinkId = snapshot.data[index].id;

                              print(hyperlinkId);

                            },
                            title: Padding(
                              padding: const EdgeInsets.only(top: 10),
                              child: Text(snapshot.data[index],
                                  style: GoogleFonts.poppins(
                                    textStyle: const TextStyle(
                                      fontWeight: FontWeight.normal,
                                      fontSize: 20,
                                    ),
                                  )),
                            ),
                            tileColor: Colors.blueGrey[200],
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10)),
                          ),
                        );
                      });
                } else {
                  return const Text("No Data");
                }
              },
            ),

snapshot.data[index] prints: snapshot.data[index] 打印:

I/flutter (13278): Code : LE-0000000002
I/flutter (13278): Description : test_01
I/flutter (13278): Organisation Unit : 01_01_04_01_SA - Shah Alam
I/flutter (13278): Date Reported : 18/09/2020
I/flutter (13278): Status : LE110 - Pending Approval
I/flutter (13278): RunHyperlink : {classid: 25510, id: 2, mad_key: 32835}

Right now snapshot.data holds List String im guessing.现在,snapshot.data 包含我猜测的列表字符串。 and snapshot.data[index] holds String.和 snapshot.data[index] 保存字符串。 The objective here is to access whatever is in "id" to use it as a variable.这里的目标是访问“id”中的任何内容以将其用作变量。

pls excuse my lack of knowledge I am new to this whole scene.请原谅我缺乏知识,我对整个场景都是新手。

Use jsonDecode() method to decode your string into json.使用jsonDecode()方法将您的字符串解码为 json。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM