easy_entry 1.0.1 copy "easy_entry: ^1.0.1" to clipboard
easy_entry: ^1.0.1 copied to clipboard

A simple, readable and concise way to deal with modifying, inserting and removing Map entries in Dart.

A simple, readable and concise way to deal with modifying, inserting and removing Map entries in Dart.

Inspired on Rust's Entry API.

Usage #

Add easy_entry to our dependencies in pubspec.yaml:

dependencies:
  easy_entry: ^1.0.0
copied to clipboard

Next, import the library:

import 'package:easy_entry/easy_entry.dart';
copied to clipboard

Get a map's entry #

Use the .entry(key) method on a map to get an entry:

final map = <int, List<String>>{};

map.entry(10);
copied to clipboard

Then, you can perform operations on your entry to modify, insert or remove it from the map:

final items = map
      .entry(10)
      .retainIf((value) => value.isNotEmpty)
      .andModify((value) => value.add('Another Item'))
      .orInsert([]);
copied to clipboard

Inserting

Use any of the orInsert methods to insert an value if there isn't a current entry with the given key:

final map = <int, String> {};

final String value1 = map.entry(1).orInsert('Item 1'); // Inserts the value if the key is not present.
final String value2 = map.entry(2).orInsertWith(() => 'Item 2'); // Lazily inserts the value.
final String value3 = map.entry(3).orInsertWithKey((key) => 'Item $key'); // Lazily inserts the value with the key.
copied to clipboard

Modifying

If you want to replace the value of the given entry, use any of the replace methods:

final map = <int, String> {
  1: 'Item 1',
  2: 'Item 2',
  3: 'Item 3',
};

final entry1 = map.entry(1).replace('User item 1');
final entry2 = map.entry(2).replaceWith(() => 'User item 2');.
final entry3 = map.entry(3).replaceWithKey((key) => 'User item $key');
final entry4 = map.entry(4).replaceWith(() => 'User item 4'); // Since there is no entry with the key 4, the entry will remain empty.
copied to clipboard

If you instead want to modify the value of the entry, like adding an item to a List, then use andModify:

final map = <int, List<String>>{
  1: ['Item 1', 'Item 2'],
};

map.andModify((value) => value.add('Item 3'));
copied to clipboard

Filtering

If you want to filter if the value of the entry is valid, you can use retainIf to retain the value if it satisfie the condition:

final map = <int, List<String>>{
  1000: ['Item 1', 'Item 2'],
};

map.entry(1000).retainIf((value) => value.isNotEmpty);
copied to clipboard

A common thing to do is to check if the value is valid, and, in the case of an invalid value, insert a default value for the entry:

final map = <int, List<String>>{
  1000: ['Item 1', 'Item 2'],
};

final items = map
      .entry(1000)
      .retainIf((value) => value.length >= 1)
      .orInsertWithKey((key) => ['Default Item $key']);
copied to clipboard

Removing

You can also remove the entry from the map, returning the value if it was present:

final map = <int, List<String>>{
  1000: ['Item 1', 'Item 2'],
};

final removedItems = map.entry(1000).remove();
copied to clipboard
3
likes
160
points
38
downloads

Publisher

unverified uploader

Weekly Downloads

2024.09.08 - 2025.03.23

A simple, readable and concise way to deal with modifying, inserting and removing Map entries in Dart.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

More

Packages that depend on easy_entry