skip to Main Content

I am trying to see if this code will work to help process the JSON I have that has a null element in it. I am doing this in DartPad just to see if this will work.

var jsonData = {
[
  {
    "addressLine1": "5500 Grand Lake Dr",
    "city": "San Antonio",
    "state": "TX",
    "zipCode": "78244",
    "formattedAddress": "5500 Grand Lake Dr, San Antonio, TX 78244",
    "assessorID": "05076-103-0500",
    "bedrooms": 3,
    "county": "Bexar",
    "legalDescription": "B 5076A BLK 3 LOT 50",
    "squareFootage": 1878,
    "subdivision": "CONV A/S CODE",
    "yearBuilt": 1973,
    "bathrooms": 2,
    "lotSize": 8843,
    "propertyType": "Single Family",
    "lastSaleDate": "2017-10-19T00:00:00.000Z",
    "features": null,
    "taxAssessment": {
      "2018": {
        "value": 126510,
        "land": 18760,
        "improvements": 107750
      },
      "2019": {
        "value": 135430,
        "land": 23450,
        "improvements": 111980
      },
      "2020": {
        "value": 142610,
        "land": 23450,
        "improvements": 119160
      },
      "2021": {
        "value": 163440,
        "land": 45050,
        "improvements": 118390
      },
      "2022": {
        "value": 197600,
        "land": 49560,
        "improvements": 148040
      }
    },
    "propertyTaxes": {
      "2019": {
        "total": 2997
      },
      "2021": {
        "total": 3468
      }
    },
    "owner": {
      "names": [
        "MICHEAL ONEAL SMITH"
      ],
      "mailingAddress": {
        "id": "149-Weaver-Blvd,-Weaverville,-NC-28787",
        "addressLine1": "149 Weaver Blvd",
        "city": "Weaverville",
        "state": "NC",
        "zipCode": "28787"
      }
    },
    "id": "5500-Grand-Lake-Dr,-San-Antonio,-TX-78244",
    "longitude": -98.351442,
    "latitude": 29.475962
  }
]
    };

jsonData.removeWhere((key, value) => key == null || value == null);

The error I am getting is on the jsonData.removeWhere() line of code:

The argument type ‘bool Function(List<Map<String, Object?>>, dynamic)’ can’t be assigned to the parameter type ‘bool Function(List<Map<String, Object?>>)’

I have a null value at the "features:" element of the map.

I tried this on a simple, one-level map and it worked.

Why am I getting this error and how do I fix it?
Thanks

3

Answers


  1. The variable is Set<List<Map<String, Object?>>>, so the removeWhere function cannot take (key, value) parameters. In other words, you are getting an error because it’s a Set variable.

    In a JSON data, keys must be unique.

    You can do the following:

    1. Remove the curly braces and use the removeWhere function inside a List.
    var jsonData = [
      {
       ...
      }
    
    jsonData.forEach((element) => element.removeWhere((key, value) => value == null));
    

    Or

    1. You can add a key to the variable and convert it to a Map.
    var jsonData = {
      null: [ //add here null or something. I recommend use a string value.
        {
         ...
    

    Hope it helps

    Login or Signup to reply.
  2. This is enough for your case.

    for (var element in jsonData) {
      element.first.removeWhere((key, value) => key == null || value == null);
    }
    
    Login or Signup to reply.
  3. Try this:

    jsonData.forEach((element) => element.forEach((map) => map.removeWhere((key, value) => key == null || value == null)));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search