【问题标题】:How to ignore an arrayList item from json response in flutter?如何在颤动中忽略json响应中的arrayList项?
【发布时间】:2022-01-12 13:22:37
【问题描述】:

这是我的演示 json 响应。假设我想显示来自"CategoryList 的所有数据,但我没有在我的应用程序中显示"CategoryName": "NEW ARRIVALS", 的列表。如何忽略来自我的 Json 响应的特定数据?我可以通过关键字New过滤掉吗?

如果New 关键字出现在CategoryName 中,将忽略整个列表

{
    "Status": 1,
    "Message": "",
    "CategoryList": [
          {
        "CategoryId": "2",
        "CategoryName": "Women",
        "CategoryImage": "https://ductImage/vkzvquiex2747i.jpg",
        "CategoryNote": "Satin Night Suits For Women",
        "SubCategoryList": [
            {
                "CategoryId": "2",
                "CategoryName": "Women",
                "SubCategoryId": "10",
                "SubCategoryName": "Satin & Lace",
                "SubCategoryImage": "https://oductImage/cxb1920565muxxb.jpg",
                "SubCategoryNote": "Ultra chic sophisticated & easy-breezy Collection"
            },               
           
            {
                "CategoryId": "2",
                "CategoryName": "Women",
                "SubCategoryId": "16",
                "SubCategoryName": "Bathrobe",
                "SubCategoryImage": "https://ProductImage/19eprae16hd3vqc.jpg",
                "SubCategoryNote": "Super-soft Bathrobe's for cosy moments at home."
            },
            {
                "CategoryId": "2",
                "CategoryName": "Women",
                "SubCategoryId": "39",
                "SubCategoryName": "Bedroom Slippers",
                "SubCategoryImage": "https:///ProductImage/bpq1tb8g2mawc18.jpg",
                "SubCategoryNote": "Leisure home room slippers."
            }
        ]
    },



        {
            "CategoryId": "12",//========Want to ignore where `CategoryId": "12`==========
            "CategoryName": "NEW ARRIVALS",
            "CategoryImage": "https://oductImage/wq35lbuzanbpzh.jpg",
            "CategoryNote": "Custom Designed & Exclusive Night Wears",
            "SubCategoryList": [
                {
                    "CategoryId": "12",
                    "CategoryName": "NEW ARRIVALS",
                    "SubCategoryId": "45",
                    "SubCategoryName": "New Arrivals For Women",
                    "SubCategoryImage": "https://n/ProductImage/llwyc6wiky73jts.jpg",
                    "SubCategoryNote": "My night My Style with Customized Women Night Suit"
                },
                {
                    "CategoryId": "12",
                    "CategoryName": "NEW ARRIVALS",
                    "SubCategoryId": "46",
                    "SubCategoryName": "New Arrivals For Men",
                    "SubCategoryImage": "https://in/ProductImage/1pj9oyq0zpgfu9c.jpg",
                    "SubCategoryNote": "Comfortable Customizable Men night Suites "
                },
                {
                    "CategoryId": "12",
                    "CategoryName": "NEW ARRIVALS",
                    "SubCategoryId": "47",
                    "SubCategoryName": "New Arrivals For Kids",
                    "SubCategoryImage": "https://.in/ProductImage/norg12vhysn5fbm.jpg",
                    "SubCategoryNote": "Be Fashionable, be divine, be yourself and Shine"
                }
            ]
        },
        {
            "CategoryId": "13",
            "CategoryName": "Bedsheet",
            "CategoryImage": "https://ductImage/r9eqe10ltoxqxo.jpg",
            "CategoryNote": "Cotton Rich Printed Bedsheets",
            "SubCategoryList": [
                {
                    "CategoryId": "13",
                    "CategoryName": "Bedsheet",
                    "SubCategoryId": "50",
                    "SubCategoryName": "Bedsheet",
                    "SubCategoryImage": "https://roductImage/e8wigvfgulr15j5.jpg",
                    "SubCategoryNote": "Ultrafine Cotton BedSheet "
                }
            ]
        },

  ]
}

我的 Ui 部分在我的应用屏幕中显示数据。

   FutureBuilder(
                future: AllCategories_SubCat(),
                builder: (BuildContext context, AsyncSnapshot snapshot) {
                  if (snapshot.connectionState != ConnectionState.done) {
                    return Center(
                        child:
                            // Lottie.network(
                            //     'https://assets1.lottiefiles.com/packages/lf20_YMim6w.json'));
                            CupertinoActivityIndicator());
                  }
                  if (snapshot.hasError) {
                    return Text(
                        "Somthing went wrong. Please try after some time.");
                  }

                  if (snapshot.hasData) {
                    return ListView.builder(
                      scrollDirection: Axis.horizontal,
                      physics: BouncingScrollPhysics(),
                      shrinkWrap: true,
                      itemCount: snapshot.data.length,
                      itemBuilder: (BuildContext context, int index) =>
                          Padding(
                        padding:
                            EdgeInsets.only(left: blockSizeHorizontal * 5),
                        child: GestureDetector(
                          onTap: () {
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (BuildContext context) =>
                                        Sub_categories_page(
                                            snapshot.data[index])));
                          },
                          child: SizedBox(
                            //width: blockSizeHorizontal * 68,
                            width: getProportionateScreenWidth(180),
                            height: blockSizeVertical * 15,..........
..........
.........

【问题讨论】:

  • 您的代码不可复制,我应该在自定义代码中向您展示吗?
  • 是的..没问题。我只需要逻辑我可以忽略这个CategoryId": "12" 列表...(我没有发布整个类文件代码,因为它很长。)
  • 您需要实际的 Api 吗?我可以尽快为您提供。
  • 没有。我只是分享一个参考,如果你不明白,请告诉我。但是你如何定义你必须隐藏的 id 呢?
  • 在我看来,应该在将列表传递给构建器之前完成转换,因为不显示类别 12 的验证会导致它无法在视觉上显示,但项目的空间将保留在查看,您可以通过将该元素放在首位并在 pos 1 开始绘画或从列表中删除该项目来进行排序

标签: json flutter api dart


【解决方案1】:

您可以在将列表发送给构建器之前将此过滤器应用于列表

 categoryList.removeWhere((element) => element.CategoryId == '12');

【讨论】:

  • 它就像魅力一样!!!!
猜你喜欢
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多