An open API service indexing awesome lists of open source software.

https://github.com/busterc/merge_map_null_safety

:dart: Dart and/or Flutter - Combine multiple Maps into one (with null safety). Equivalent to Object.assign in JS.
https://github.com/busterc/merge_map_null_safety

Last synced: 6 months ago
JSON representation

:dart: Dart and/or Flutter - Combine multiple Maps into one (with null safety). Equivalent to Object.assign in JS.

Awesome Lists containing this project

README

          

# merge_map_null_safety

> Combine multiple Maps into one ([with null safety](https://dart.dev/null-safety)). Equivalent to
[Object.assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
in JS.

## Example

```dart
import "package:merge_map_null_safety/merge_map.dart";

/// Map mergeMap(Iterable> maps, {bool recursive: true, bool acceptNull: false})
///
/// Merges the values of the given maps together.
///
/// `recursive` is set to `true` by default. If set to `true`,
/// then nested maps will also be merged. Otherwise, nested maps
/// will overwrite others.
///
/// `acceptNull` is set to `false` by default. If set to `false`,
/// then if the value on a map is `null`, it will be ignored, and
/// that `null` will not be copied.

main() {
Map map1 = {'hello': 'world', 'bye': null};
Map map2 = {'foo': {'bar': 'baz', 'this': 'will be overwritten'}};
Map map3 = {'foo': {'john': 'doe', 'this': 'overrides previous maps'}};

Map mergedWithNulls = mergeMap([map1, map2, map3], acceptNull: true);
print(mergedWithNulls);
// {hello: world, bye: null, foo: {bar: baz, john: doe, this: overrides previous maps}}

Map mergedWithoutNulls = mergeMap([{}, map1]);
print(mergedWithoutNulls);
// {hello: world}
}

```