![](/img/trans.png)
[英]I am getting a flutter type error 'Response' is not a subtype of 'FutureOr<String>'
[英]I am getting an Error when I save to a String List. Flutter/Dart
在终端中打印我的列表很好,但是当我想将它保存到缓存时,我收到了这个错误:
Error: The argument type 'List<Map<String, dynamic>>' can't be assigned to the parameter type 'List<String>'.
错误指向:
await WriteCache.setListString(key: 'cache4', value: incidentList);
这是我的代码:
onTap: () async {
var newMessage = await (ReadCache.getString(key: 'cache1'));
var response = await http.get(
Uri.parse(
'http://192.168.1.8:8080/HongLeong/MENU_REQUEST.do?_dc=1658076505340&reset_context=1&table_id=25510&id_MenuAction=3&path=%3Cp%20class%3D%22x-window-header-path%22%3ELoss%20Event%3C%2Fp%3E&ViewType=MENU_REQUEST&gui_open_popup=1&id_Window=17&activeWindowId=mw_17&noOrigUserDate=true&LocalDate=20220718&LocalTime=00482500&TimeZone=Asia%2FShanghai&UserDate=0&UserTime=0&server_name=OPRISK_DATACOLLECTOR&key_id_list=&cell_context_id=0&id_Desktop=100252&operation_key=1000184&operation_sub_num=-1&is_json=1&is_popup=0&is_search_window=0&ccsfw_conf_by_user=0&is_batch=0&previousToken=1658069547560&historyToken=1658076505339&historyUrl=1'),
headers: {HttpHeaders.cookieHeader: newMessage},
);
LossEventResponseModel lossEventResponseModel =
LossEventResponseModel.fromJson(jsonDecode(response.body));
final listNode = lossEventResponseModel.response.genericListAnswer.listNode;
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'][46]['field_value'],
'Date Reported': json['field'][18]['field_value'],
}
];
await WriteCache.setListString(key: 'cache4', value: incidentList);
print(incidentList);
Navigator.of(context).push(
MaterialPageRoute(builder: (context) => const LossEvent()));
},
这就是我要打印的内容:
[{Code: LE-0000000002, Description: test_01, Organisation Unit: 01_01_04_01_SA - Shah Alam, Date Reported: 18/09/2020}, {Code: LE-0000000003, Description: Transactions not reported (intentional), Organisation Unit: 01_01_04_01_HQ - Menara Hong Leong, Damansara City, Date Reported: 03/02/2018},and so on.....
我真的很想将其显示为:
Code: LE-0000000002,
Description: test_01,
Organisation Unit: 01_01_04_01_SA - Shah Alam,
Date Reported: Date Reported: 18/09/2020,
但错误阻止我这样做。 任何有关修复我的代码的建议都会很棒!
您可以检查setListString
获取键和字符串列表
static Future setListString(
{required String key, required List<String> value})
但是您正在尝试放置地图列表
List<Map<String, dynamic>> incidentList
你可以做类似的事情
final List<String>values = [];
for(final item in incidentList){
values.addAll(item.values.map((e) => e.toString()));
}
await WriteCache.setListString(key: 'cache4', value: values);
更多关于cache_manager
WriteCache.setListString()
需要一个List<String>
类型的值,但 eventList 的类型是List<Map<String, dynamic>>
。
相反,考虑将您的列表编码为 JSON,并使用WriteCache.setString()
存储它:
// Top of file:
import 'dart:convert';
// In onTap:
final incidentListJson = jsonEncode(incidentList);
await WriteCache.setString(key: 'cache4', value: incidentListJson);
注意:读取缓存时,您还需要使用decodeJson
将 JSON 字符串转换回列表:
final incidentListJson = await ReadCache.getString(key: 'cache4');
final List incidentList = jsonDecode(incidentListJson);
如果您希望列表按照您显示的方式进行格式化,您可以为此定义一个函数:
String formatIncidentList(List<Map<String, dynamic>> list) {
return list.map(
(incident) => incident.entries.map(
(entry) => "${entry.key}: ${entry.value},"
),
).join("\n");
}
// Usage:
print(formatIncidentsList(incidentList));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.