当有数据时显示警告框

问题描述 投票:0回答:1

我具有带图像和一些详细信息的Listview,如果没有数据,它将返回一个警告框,其中显示未找到任何数据的消息,但就我而言,即使服务器中存在数据,警告框也会显示。警报框会显示服务器中是否没有数据,还会显示何时存在数据

如果没有数据,将显示此信息

{
  "status": true,
  "Image": []
}

如果有数据

{
  "status": true,
  "doc": [
    {
      "Description": "test",
      "replay": " ",
      "replay_status": 0,
      "_id": "5ece4f405013d004a83385cd",
      "image_path": "http://xxx.xxx.xxx.xxx:xxxx/area_images/1615676104790.jpg",
      "Branch_Id": {
        "_id": "5e27ffb7cde0456455857c1f",
        "name": "ALLEPPEY"
      },
      "image_temp_path": "http://xxx.xxx.xxx.xxx:xxxx/area_images_temp/1615676104790.jpg",
      "Order_Id": 1138,
      "Area_Id": {
        "_id": "5e54c20f93fe90548801354a",
        "Area_Name": "For Testing"
      },
      "Created_At": "Wednesday, May 27th, 2020, 5:00:07 PM"
    },

  }

这是检索图像的功能

 Future<String> getImageList(String set_id) async {

            ProgressDialog dialog = CustomDialogs().showLoadingProgressDialog(context);

                 var response = await http.post(Urls.ImageList,
                      headers: {
                        "Content-Type": "application/json",
                        "Authorization": "Bearer $token",
                      },
                      body: json.encode({
                        "sets_id": set_id,

                      }));

                  if (response.statusCode == 200) {

                    dialog.dismissProgressDialog(context);

                    try {
                      var resp = response.body;

                      if(resp.length < 0)
                        {
                          Map<String, dynamic> value = json.decode(resp);
                          var report = value['Image'];

                          for (int i = 0; i < report.length; i++) {
                            var data = report[i];
                            var SetName = data["Set_Id"]["Set_Name"];

                            Imagelist.add(ImageModel.fromJson(data, SetName , ));
                          }

                          setState(() {
                            array_lenth = Imagelist.length;
                          });
                        }
                      else
                        {
                          Alert(
                            context: context,
                            type: AlertType.error,
                            title: "No Data",
                            desc: "No Avilable Data Found",
                            buttons: [
                              DialogButton(
                                child: Text(
                                  "OK",
                                  style: TextStyle(color: Colors.white, fontSize: 20),
                                ),
                                onPressed: () => Navigator.pop(context),
                                width: 120,
                              )
                            ],
                          ).show();
                        }

                    } catch (e) {
                      e.toString();
                    }
                  }
                  else {
                    print("Error");
                  }
              }
          }
flutter dart flutter-layout
1个回答
0
投票

我已经通过解码json解决了这个问题,并使用该对象并将其放入名为report的变量中,然后检查了报告的长度。

 try {
          var resp = response.body;
          Map<String, dynamic> respo = json.decode(resp);
          var report = respo['doc'];

          if(report.length > 0)  {

            Map<String, dynamic> value = json.decode(resp);
            var report = value['doc'];

            for (int i = 0; i < report.length; i++) {
              var data = report[i];
              var areaName = data["Area_Id"]["Area_Name"];
              var branchName = data["Branch_Id"]["name"];
              Imagelist.add(
                  ImageModel.fromJson(data, areaName, branchName));
            }

            setState(() {
              array_lenth = Imagelist.length;
            });
          } else {
            Alert(
              context: context,
              type: AlertType.error,
              title: "No Data Found",
              desc: "No Images Data Available",
              buttons: [
                DialogButton(
                  child: Text(
                    "OK",
                    style: TextStyle(color: Colors.white, fontSize: 20),
                  ),
                  onPressed: () => Navigator.pop(context),
                  width: 180,
                  color: Colors.green,
                ),
              ],
            ).show();
          }
        } catch (e) {
          e.toString();
        }
© www.soinside.com 2019 - 2024. All rights reserved.