0

I'm trying to insert an object into array of objects. The idea is just add the content of the object if the object already exist but I don't understand why I can't use the .insert

I have tried using the .insert and adding the object into the array.

void main() {

  var arr = [
    {
      "shelf1": [
        {
          "Entry": "something",
          "Status": "",
          "Account": "",
          "Address": "",
          "Result": null
        }
      ]
    },
    {
      "shelf2": [
        {
          "Entry": "something",
          "Status": "",
          "Account": "",
          "Address": "",
          "Result": null
        }
      ]
    },
    {
      "shelf3": [
        {
          "Entry": "something",
          "Status": "",
          "Account": "",
          "Address": "",
          "Result": null
        }
      ]
    }
  ];

  var object = {
    "shelf1": {
      "Entry": "something2",
      "Status": "",
      "Account": "",
      "Address": "",
      "Result": null
    }
  };

  arr.insert(0, object);

  print(arr);
}
1

1 Answer 1

1

The arr has a type of List<Map<String, Map<String, List<Map<String, String>>>>> and the object you declared has a type of Map<String, Map<String, String>>. This is because using var, the type is automatically inferred based on the assigned value. Since you're trying to add an object of an incorrect type an error is thrown.

So your new object should be made like the following to be inserted normally:

var object = {
  "shelf1": [{         // note that I made this inside a List []
    "Entry": "something2",
    "Status": "",
    "Account": "",
    "Shelf": "shelf",
    "Address": "",
    "Result": null
  }]
};
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.