Update list in dart

Dart: How to Update a Map

Last updated on August 17, 2021 A Goodman Loading... Loading... Post a comment
report this ad

This short article shows you 3 ways to update a map in Dart [and Flutter as well].

Table of Contents

  • 1 Using map[key] = [newValue] syntax
  • 2 Using update[] method
  • 3 Using updateAll[] method
  • Whats Next?

1 Using map[key] = [newValue] syntax

This approach is straightforward and makes the code easy to read and write.Advertisements

Example:

// main.dart Map person = { 'firstName': 'John', 'lastName': 'Doe', 'age': 45, 'spouse': 'Jane Doe', 'children': 3, 'nationality': 'Unknown' }; void main[] { person['nationality'] = 'United States'; person['spouse'] = 'KindaCode.com'; person['children'] = 5; print[person]; }

Output:

{ firstName: John, lastName: Doe, age: 45, spouse: KindaCode.com, children: 5, nationality: United States }

2 Using update[] method

This built-in method will update the value associated with a given key.

map.update[key, [value] => newValue]

Example:

// main.dart Map map1 = { 'key1': 'Kindacode.com', 'key2': 'Blue', 'key3': 'Green', 'key4': 'Orange' }; void main[] { map1.update['key3', [value] => 'Transparent']; map1.update['key4', [value] => 'Something New']; print[map1]; }

Output:

{ key1: Kindacode.com, key2: Blue, key3: Transparent, key4: Something New }

3 Using updateAll[] method

Example

Advertisements

This example will update the values of the map by their squared:

// main.dart Map map2 = {'key1': 1, 'key2': 3, 'key3': 9, 'key4': 18}; void main[] { map2.updateAll[[key, value] => value * value]; print[map2]; }

Output:

{ key1: 1, key2: 9, key3: 81, key4: 324 }

Whats Next?

Further reading:

  • Dart & Flutter: 2 Ways to Count Words in a String
  • Base64 encoding and decoding in Dart [and Flutter]
  • How to remove items from a list in Dart
  • Dart: Convert Timestamp to DateTime and vice versa
  • Dart & Flutter: Get the Index of a Specific Element in a List
You can also take a tour around our Flutter topic page and Dart topic page to see the latest tutorials and examples.
Advertisements
Share Tweet Telegram
Subscribe
Notify of
I allow to use my email address and send notification about new comments and replies [you can unsubscribe at any time].
Label
{} [+]
Name*
Email*
Label
{} [+]
Name*
Email*
0 Comments
Inline Feedbacks
View all comments

You May Also Like

Dart: Convert Class Instances [Objects] to Maps and Vice Versa

January 7, 2022

How to Flatten a Nested List in Dart

December 3, 2021

Inheritance in Dart: A Quick Example

December 3, 2021

Flutter & Dart: 3 Ways to Generate Random Strings

November 24, 2021

Flutter & Dart: Displaying Large Numbers with Digit Grouping

November 7, 2021

Using Static Methods in Dart and Flutter

October 1, 2021

Prevent VS Code from Auto Formatting Flutter/Dart Code

August 24, 2021

Dart: Converting a List to a Map and Vice Versa

August 19, 2021

Dart: Checking whether a Map is empty

August 17, 2021

Dart: Checking if a Map contains a given Key/Value

August 17, 2021

Dart: How to Add new Key/Value Pairs to a Map

August 17, 2021

Dart: How to remove specific Entries from a Map

August 17, 2021

Video liên quan

Chủ Đề