Home > Mobile >  I am getting an Error when I save to a String List. Flutter/Dart
I am getting an Error when I save to a String List. Flutter/Dart

Time:07-20

Printing my List is fine in the terminal but when I want to save it to the Cache, I am getting this error :

Error: The argument type 'List<Map<String, dynamic>>' can't be assigned to the parameter type 'List<String>'.

The Error is pointing towards :

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

This is my code :

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=

Loss Event

&ViewType=MENU_REQUEST&gui_open_popup=1&id_Window=17&activeWindowId=mw_17&noOrigUserDate=true&LocalDate=20220718&LocalTime=00482500&TimeZone=Asia/Shanghai&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())); },

This is what I am printing :

 [{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.....

I would really love to display it as :

Code: LE-0000000002,
Description: test_01,
Organisation Unit: 01_01_04_01_SA - Shah Alam,
Date Reported: Date Reported: 18/09/2020,

but the error is stopping me from doing so. Any advice on fixing my code would be great!

CodePudding user response:

You can check setListString takes key and list of String

 static Future setListString(
      {required String key, required List<String> value})

But you are trying to put list of map

List<Map<String, dynamic>> incidentList

You can do something like

final List<String>values =  [];

for(final item in incidentList){
values.addAll(item.values.map((e) => e.toString()));
}
await WriteCache.setListString(key: 'cache4', value: values);

More about cache_manager

CodePudding user response:

Storing the List

WriteCache.setListString() expects a value of type List<String>, but incidentList has the type List<Map<String, dynamic>>.

Instead, consider encoding your list as JSON, and store it using WriteCache.setString():

// Top of file:
import 'dart:convert';

// In onTap:
final incidentListJson = jsonEncode(incidentList);
await WriteCache.setString(key: 'cache4', value: incidentListJson);

Note: When reading the cache, you will also need to use decodeJson to convert the JSON string back into a list:

final incidentListJson = await ReadCache.getString(key: 'cache4');
final List incidentList = jsonDecode(incidentListJson);

Formatting the List

If you'd like the list to be formatted in the way you showed, you can define a function for that:

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));
  • Related