How to add key value-pair to a Object?
10,155
You can assign to a Map using the indexing operator
options['middle_name'] = 'Kumar';
{}
is a Map
literal to create a Map
instance.
The result allows you to use all methods of Map
like remove
![nitishk72](https://i.stack.imgur.com/t5KML.png?s=256&g=1)
Author by
nitishk72
A developer who loves to work with various technology. Always ready to learn and explore new technologies. Expertise Primary: JavaScript. Secondary: Flutter Social My Website Youtube Channel GitHub Twitter
Updated on June 07, 2022Comments
-
nitishk72 about 2 years
I want to update my Object by adding a more key-value pair.
Object options = { "first_name": "Nitish", "last_name" : "Singh" }
after initializing the Object I want to add one more key and value. Is there any way to do this.
after adding one more key-value pair my object will look like this
options = { "first_name" : "Nitish", "last_name" : "Singh" "middle_name": "Kumar" }
-
nitishk72 about 6 yearsI got this error : The operator '[]=' isn't defined for the class 'Object'
-
Günter Zöchbauer about 6 yearsThat's an analyzer warning. Use
var
orfinal
instead ofObject
to have the type inferred or useMap
orMap<String,String>
to specify a concrete type yourself. See also my updated answer. -
Ganymede about 6 yearsIf you don't want to overwrite a value if the key already exists, then there is also
map.putIfAbsent(key, () => valueIfAbsent);