Home > Back-end >  How do I list down two positions from an array in one variable. Flutter/dart
How do I list down two positions from an array in one variable. Flutter/dart

Time:07-18

I want to combine the my variables instead of having two as shown in my code.

I tried doing this but it doesnt seem to work

var table = responseModel.response.genericListAnswer.listNode
                          .map((x) => x.toJson()['field'][0]['field_value'][5]['field_value']); 

This is my code :

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=1657717579436&table_id=25018&fk_table_id=25004&id_MenuAction=3&reset_context=1&ViewType=MENU_REQUEST&gui_open_popup=1&id_Window=5&activeWindowId=mw_5&noOrigUserDate=true&LocalDate=20220713&LocalTime=21061900&TimeZone=Asia/Shanghai&UserDate=0&UserTime=0&server_name=OPRISK_DATACOLLECTOR&key_id_list=&cell_context_id=0&id_Desktop=100237&operation_key=1000007&operation_sub_num=-1&is_json=1&is_popup=0&is_search_window=0&ccsfw_conf_by_user=0&is_batch=0&previousToken=1657717554097&historyToken=1657717579434&historyUrl=1'),
                        headers: {HttpHeaders.cookieHeader: newMessage},
                      );
                      ResponseModel responseModel =
                      ResponseModel.fromJson(jsonDecode(response.body));

                      var table = responseModel.response.genericListAnswer.listNode
                          .map((x) => x.toJson()['field'][5]['field_value']);

                      var message = responseModel.response.genericListAnswer.listNode
                          .map((x) => x.toJson()['field'][0]['field_value']);


                      var messageList = new List<String>.from(message);

                      var tableList = new List<String>.from(table);

                      WriteCache.setListString(key : "cache3", value: messageList);
                      WriteCache.setListString(key : "cache4", value: tableList);

                      print(messageList);

                      Navigator.of(context).push(MaterialPageRoute(
                          builder: (context) =>  messageBoard()));

                    },

this is the error that I am getting :

Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'

this is the JSON :

 "field":[
            {
              "field_name":"common_desc_0",
              "col_index":"1",
              "field_value":"There are 1 Loss Event currently in the status LE110 - Pending Approval",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"btime_touched",
              "col_index":"2",
              "field_value":"07/02/2022 04:32:37",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"date_end",
              "col_index":"3",
              "field_value":"",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"desc_board",
              "col_index":"4",
              "field_value":"There are 1 Loss Event currently in the status LE110 - Pending Approval",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"email_status",
              "col_index":"5",
              "field_value":"1",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"foreign_key",
              "col_index":"6",
              "field_value":"Loss Event",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"id_ApplTable",
              "col_index":"7",
              "field_value":"LossEvent - Loss Event",
              "mad_key":"25510",
              "id":"25510"
            },
            {
              "field_name":"id_UsersxFrom",
              "col_index":"8",
              "field_value":"",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"id_UsersxTo",
              "col_index":"9",
              "field_value":"manager_01",
              "mad_key":"100004",
              "id":"100002"
            },
            {
              "field_name":"index_true_last",
              "col_index":"10",
              "field_value":"1",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"note",
              "col_index":"11",
              "field_value":"There are 1 Loss Event currently in the status LE110 - Pending Approval:\r\nThere are some objects which need your attention1) LE-0000000002 - test_01\r\n",
              "mad_key":"0",
              "id":"0"
            },
            {
              "field_name":"read_To",
              "col_index":"12",
              "field_value":"No",
              "mad_key":"0",
              "id":"0",
              "boolean_value":"0"
            }
          ]

Im trying to show the "field_value" from [0] and [5] at the same time in one variable instead of two.

CodePudding user response:

Unfortunately, Dart still doesn't support pairs/tuples natively. The easiest way to do a pair list-like structure is by creating a Map like so:

final listNode = responseModel.response.genericListAnswer.listNode;

// Create a pair list-like structure with Map
Map<String, dynamic> tableMessages = {
    for (final json in listNode.map((x) => x.toJson()))
      json['field'][5]['field_value']: json['field'][0]['field_value']
  };

Take care with this approach because Maps doesn't support key duplicates. But looks like in your case it fits like a glove because table is a foreign key and keys are unique.

To write it to the cache use WriteCache.setJson instead of WriteCache.setListString like the following:

  await WriteCache.setJson(key: 'cache4', value: tableMessages);

For the curious, the error Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index' is related to one of the String indexes: ['field'], the first ['field_value'], or the last ['field_value']. One of those is actually a List and a List should only be indexed by an int.

  • Related