[英]How do I list down two positions from an array in one variable. Flutter/dart
我想組合我的變量而不是像我的代碼中顯示的那樣有兩個。
我試過這樣做,但它似乎不起作用
var table = responseModel.response.genericListAnswer.listNode
.map((x) => x.toJson()['field'][0]['field_value'][5]['field_value']);
這是我的代碼:
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()));
},
這是我得到的錯誤:
未處理的異常:“String”類型不是“index”的“int”類型的子類型
這是 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"
}
]
我試圖在一個變量而不是兩個變量中同時顯示 [0] 和 [5] 中的“field_value”。
不幸的是,Dart 本身仍然不支持對/元組。 做一個pair list-like結構的最簡單的方法是創建一個像這樣的Map
:
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']
};
請注意這種方法,因為Maps
不支持密鑰重復。 但是在您的情況下,它看起來像手套一樣,因為table
是外鍵並且鍵是唯一的。
要將其寫入緩存,請使用WriteCache.setJson
而不是WriteCache.setListString
,如下所示:
await WriteCache.setJson(key: 'cache4', value: tableMessages);
出於好奇,錯誤Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'
與String
索引之一相關: ['field']
,第一個['field_value']
,或最后一個['field_value']
。 其中之一實際上是List
, List
只能由int
索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.