Search code examples
jsonflutterelasticsearchalgoliaflutter-getx

how to filter json data with multiple conditions in flutter?


Hi all I want to add one more condition to filter json list more effectively in flutter. And my json looks like this

    {
    "product": "butox 50 ml",
    "agencies": [
        {
            "agency": "Sai Ganesh",
            "agency uid": "8"
        },
        {
            "agency": "slv",
            "agency uid": "10"
        }
    ]
},
{
    "product": "voveran sr 100",
    "agencies": [
        {
            "agency": "Sree Enterprises",
            "agency uid": "31"
        }
    ]
}

below is my code I want to filter product name and from one particular agency based on uid

      List<mmamodel> results = [];
  if (productName.isEmpty) {
    results = mmaData;
  } else {
    results = mmaData
        .where((element) => element.product
                .toString()
                .toLowerCase()
                .contains(productName.toLowerCase())
                 &&
             element.agencies!
                 .where((agency) => agency.agencyUid
                     .toString()
                     .toLowerCase()
                     .contains(Uid.toLowerCase()))
                 .toList().)
            
        .toList();
  }
  foundProduct.value = results;

Solution

  • results = mmaData
            .where((element) =>
                element.product
                    .toString()
                    .toLowerCase()
                    .startsWith(playerName.toLowerCase()) &&
                element.agencies!.any((agency) => agency.agencyUid == Uid))
            .toList();