0

The method 'push' isn't defined for the type 'Object'. I'm getting this error if I try to add a another procedure in the values array.

void main() {
  var myset = {
           "procedureResult": {
            "procedureSections": [
                                  {
                                    "section": "BasicInformation",
                                     "values": [
                                          {
                                             "procedure": "Angio"
                                          }
                                         ]
                                   }
                                  ]
                                }
                              };

   myset['procedureResult']!['procedureSections']![0]['values']!.push({'procedure2':'MRI'})
   print(myset['procedureResult']!['procedureSections']![0]['values']!.push({'procedure2':'MRI'}));
     }

1 Answer 1

2

Add type for the list using angel brackets and use .add method instead of .push.

var myset = {
  "procedureResult": {
    "procedureSections": <Map>[ // like this
      {
        "section": "BasicInformation",
        "values": [
          {"procedure": "Angio"}
        ]
      }
    ]
  }
};

myset['procedureResult']!['procedureSections']![0]['values']!.add({'procedure2': 'MRI'});
print(myset['procedureResult']!['procedureSections']![0]['values']);

Or use as to cast procedureSections's value to a List and use .add method instead of .push..

var myset = {
  "procedureResult": {
    "procedureSections": [
      {
        "section": "BasicInformation",
        "values": [
          {"procedure": "Angio"}
        ]
      }
    ]
  }
};

(myset['procedureResult']!['procedureSections']![0]['values']! as List).add({'procedure2': 'MRI'});
print(myset['procedureResult']!['procedureSections']![0]['values']);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.