簡體   English   中英

保存到字符串列表時出現錯誤。 顫振/飛鏢

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM