简体   繁体   English

我正在尝试在列表视图中显示我的 JSON 响应。 颤振/飞镖

[英]I am trying to display my JSON response in a List View. Flutter/Dart

This is the code to obtain the response and store it in a cache :这是获取响应并将其存储在缓存中的代码:

onPressed: () 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));

                    final listNode = responseModel.response.genericListAnswer.listNode;

                    Map<String, dynamic> tableMessages = {
                      for (final json in listNode.map((x) => x.toJson()).where((json) => json['field'][5]['field_value'] == 'Loss Event'))
                        "Message": json['field'][0]['field_value'],
                    };

                    await WriteCache.setString(key: 'cache3', value: tableMessages['Message']);
                    print(tableMessages);

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

                    }

I am printing the intended message successfully :我正在成功打印预期的消息:

I/flutter (27053): {Message: There are 1 Loss Event currently in the status LE110 - Pending Approval}

but this is what I see on the device :但这是我在设备上看到的: 在此处输入图像描述

and here is the code to display the list :这是显示列表的代码:

body: Row(
        children: [
          Expanded(
            child: FutureBuilder(
              future: ReadCache.getString(key: "cache3"),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return ListView.builder(
                      itemCount: snapshot.data.length,
                      itemBuilder: (context, index) {
                        return Padding(
                          padding: const EdgeInsets.all(5.0),
                          child: ListTile(
                            title: Text(
                                snapshot.data[index],
                              style: GoogleFonts.poppins(
                                textStyle : const TextStyle(
                                  fontWeight: FontWeight.normal,
                                  fontSize: 20,
                                ),
                              )
                            ),
                            tileColor: Colors.blueGrey[200],
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10)),
                          ),
                        );
                      });
                } else {
                  return const Text("No Data");
                }
              },
            ),
          ),
        ],
      ),

how do I display the response normally instead of separate elements like in the image provided?如何正常显示响应,而不是像提供的图像中那样显示单独的元素? Thanks.谢谢。

cache3 is storing just a string, ie the Message string. cache3只存储一个字符串,即Message字符串。 In the itemCount property the length of the Message string is assigned as the number of items.itemCount属性中, Message字符串的长度被指定为项目数。 Just change it to 1 and change the way it's accessing the text too.只需将其更改为1并更改它访问文本的方式。

It's going to be like the following:它将如下所示:

body: Row(
        children: [
          Expanded(
            child: FutureBuilder<String>(
              future: ReadCache.getString(key: "cache3"),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  return ListView.builder(
                      itemCount: 1,                     // <- Here
                      itemBuilder: (context, index) {
                        return Padding(
                          padding: const EdgeInsets.all(5.0),
                          child: ListTile(
                            title: Text(
                              snapshot.data,            // <- Here
                              style: GoogleFonts.poppins(
                                textStyle : const TextStyle(
                                  fontWeight: FontWeight.normal,
                                  fontSize: 20,
                                ),
                              )
                            ),
                            tileColor: Colors.blueGrey[200],
                            shape: RoundedRectangleBorder(
                                borderRadius: BorderRadius.circular(10)),
                          ),
                        );
                      });
                } else {
                  return const Text("No Data");
                }
              },
            ),
          ),
        ],
      ),

On the other hand, if the intention was to have multiple messages the way they are populated should be changed as the last one will always win.另一方面,如果打算有多个消息,则应该更改它们的填充方式,因为最后一个总是获胜。

    List<String> messages = [
      for (final json in listNode
          .map((x) => x.toJson())
          .where((json) => json['field'][5]['field_value'] == 'Loss Event'))
        json['field'][0]['field_value'],
    ];

And save/restore it as a string list:并将其保存/恢复为字符串列表:

await WriteCache.setStringList(key: 'cache3', value: messages);
    future: ReadCache.getStringList(key: "cache3")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想将JSON数据传递给与Web视图相关的javascript函数。 但是我无法从Javascript函数中获取响应数据 - I want to pass JSON data to javascript function related to web view. But I am unable to get the response data from the Javascript function 如何在 Dart\Flutter 中使用我的 JSON 响应 - How to use my JSON response in Dart\Flutter 我正在尝试在UITableViewCell中显示json的响应,但无法正常工作 - I am trying to display response of json in UITableViewCell but not working 如何显示对视图的JSON响应,我在做什么错? - How to display JSON response to the view, what am i doing wrong? 如何在 Flutter 的 ListView 中的 JSON 响应中显示嵌套列表(以下 json 响应中的 contactArr 列表)的内容? - How can I display the contents of nested list (contactArr list in the following json response) in JSON response in ListView in Flutter? 我如何解析这种具有不同名称的 json,或者告诉我如何在回收站视图中显示此响应。 :( - How do I parse this kinda json which has different names , or tel me how do I even display this response in recycler view. :( Flutter / Dart - JSON 在小部件中显示的列表 - 仅显示一个值 - Flutter / Dart - JSON list to display in widget - shows only one value Dart/Flutter JSON 解析和显示 - Dart / Flutter JSON parsing and display 我试图在Spring MVC中获取JSON响应以显示在前端? - I am trying to get JSON response in spring mvc to display in front end? 我是 Flutter 的新手。我想将 JSON 数据显示为 flutter 中的列表,但我不知道如何编码以将 JSON 数据显示为 Flutter 中的列表 - I am New in Flutter. I want to display JSON Data as List in flutter but I do not know how to Coding for show JSON data as a List in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM