You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter/Dart如何从缓存字符串列表中获取RunHyperlink字段?

解决方案:单独获取RunHyperlink字段内容

方案一:优化缓存存储(推荐)

当前你把incidentList的Map拼接成字符串存入缓存,导致后续无法直接提取单个字段。更合理的方式是保留原始Map结构,将每个Map转为JSON字符串存储:

1. 修改sideDrawerpage.dart的缓存逻辑

替换原有的values生成代码,直接将每个Map转为JSON字符串:

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']
    }
];

// 直接将每个Map转为JSON字符串,替代拼接逻辑
final List<String> values = incidentList.map((item) => jsonEncode(item)).toList();
await WriteCache.setListString(key: 'cache4', value: values);

2. 修改lossEvent_page.dart的列表构建逻辑

读取缓存后,将JSON字符串转回Map,即可直接获取RunHyperlink字段:

FutureBuilder(
  future: ReadCache.getStringList(key: 'cache4'),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      return ListView.builder(
          itemCount: snapshot.data.length,
          itemBuilder: (context, index) {
            // 将JSON字符串转回原始Map结构
            final incidentMap = jsonDecode(snapshot.data[index]) as Map<String, dynamic>;
            // 直接提取RunHyperlink
            final runHyperlink = incidentMap['RunHyperlink'];
            
            return Padding(
              padding: const EdgeInsets.all(8.0),
              child: ListTile(
                onTap: () {
                  print(runHyperlink);
                  // 后续请求可直接使用runHyperlink变量
                },
                title: Padding(
                  padding: const EdgeInsets.only(top: 10),
                  child: Text(
                    // 拼接成原来的显示文本
                    incidentMap.entries.map((entry) => "${entry.key} : ${entry.value}").join("\n"),
                    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");
    }
  },
);

方案二:解析现有拼接字符串(临时应急)

如果暂时不想修改缓存逻辑,可以通过字符串分割提取RunHyperlink字段,但这种方式依赖字符串格式的稳定性,一旦格式变化就会失效:

// 在lossEvent_page.dart的ListTile onTap中修改
onTap: () {
  final content = snapshot.data[index];
  // 按行分割内容
  final lines = content.split("\n");
  // 找到以RunHyperlink开头的行
  final runHyperlinkLine = lines.firstWhere(
    (line) => line.startsWith("RunHyperlink : "),
    orElse: () => "",
  );
  // 提取冒号后的内容
  final runHyperlink = runHyperlinkLine.replaceFirst("RunHyperlink : ", "").trim();
  print(runHyperlink);
  
  // 如果需要转为Map用于请求,可再解析:
  // final runHyperlinkMap = jsonDecode(runHyperlink) as Map<String, dynamic>;
},

内容的提问来源于stack exchange,提问作者JoshuaaMarkk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 10:33:19