magic_map 1.0.2 copy "magic_map: ^1.0.2" to clipboard
magic_map: ^1.0.2 copied to clipboard

A Dart package that allows JavaScript-like dot-access to deeply nested Maps and Lists.


# ๐Ÿ”ฎ MagicMap

`MagicMap` is a powerful Dart utility for dynamic, nested map access and mutation with dot-path syntax, glob support, immutable updates, and dynamic field access.

> Effortlessly manage deeply nested structures in a readable, type-safe-ish way.

---

## โœจ Features

- โœ… Dot-path access to deeply nested fields
- โœ… Dynamic creation of nested structures with `set`
- โœ… Immutable update API with `setImmutable`
- โœ… Bash-style glob pattern querying (`Glob`)
- โœ… JSON (de)serialization
- โœ… Dynamic dot access via `noSuchMethod`
- โœ… Supports `Map<String, dynamic>` and nested `List`s

---

## ๐Ÿ“ฆ Installation

Add this to your `pubspec.yaml`:

```yaml
dependencies:
  glob: ^2.1.2

Then run:

dart pub get

๐Ÿš€ Quick Start #

7๏ธโƒฃ Dynamic Dot Access (noSuchMethod) #

final map = MagicMap({'person': {'name': 'Zion'}});
print(map.person.name); // Output: Zion

๐Ÿ” Usage Examples #

1๏ธโƒฃ Basic Access #

final map = MagicMap({'name': 'Echezona', 'age': 24});
print(map.getPath('name')); // Output: Echezona
print(map.getPath('age'));  // Output: 24

2๏ธโƒฃ Setting a Nested Value Dynamically #

final map = MagicMap({});
map.set('user.profile.name', 'Miracle');

print(map.getPath('user.profile.name')); // Output: Miracle
print(map.toJsonString()); // {"user":{"profile":{"name":"Miracle"}}}

3๏ธโƒฃ Immutable Update (Functional Style) #

final map1 = MagicMap({'config': {'theme': 'dark'}});
final map2 = map1.setImmutable('config.theme', 'light');

print(map1.getPath('config.theme')); // Output: dark
print(map2.getPath('config.theme')); // Output: light

4๏ธโƒฃ Glob Matching (Flexible Pattern Matching) #

final data = {
  'settings': {
    'ui': {'theme': 'dark', 'font': 'Roboto'},
    'notifications': {'email': true, 'sms': false},
  }
};

final map = MagicMap(data);

final matches = map.getWithGlob('settings.ui.*');
print(matches); // Output: [dark, Roboto]

5๏ธโƒฃ Using Lists with Glob #

final data = {
  'users': [
    {'name': 'Alice', 'age': 30},
    {'name': 'Bob', 'age': 25},
  ]
};

final map = MagicMap(data);

final names = map.getWithGlob('users.[*].name');
print(names); // Output: [Alice, Bob]

6๏ธโƒฃ JSON String Input/Output #

final jsonStr = '{"app":{"version":"1.0.0","debug":false}}';
final map = MagicMap.fromJsonString(jsonStr);

print(map.getPath('app.version')); // Output: 1.0.0

map.set('app.debug', true);
print(map.toJsonString()); 
// Output: {"app":{"version":"1.0.0","debug":true}}

๐Ÿงฑ API Overview #

Constructor #

MagicMap(dynamic data)

Get value at dot path #

map.getPath('config.theme');

Set value at dot path #

map.set('config.theme', 'light');

Immutable update #

final newMap = map.setImmutable('user.active', true);
map.getWithGlob('settings.ui.*');

Convert to JSON #

final jsonStr = map.toJsonString();

Create from JSON #

final map = MagicMap.fromJsonString(jsonStr);

โ—Error Handling #

Throws MagicMapException on:

  • Accessing missing keys
  • Using getPath on non-Map structure
  • Invalid root data for path operations

๐Ÿ”š License #

MIT โ€” use freely, modify wildly, and contribute happily!


๐Ÿ‘จโ€๐Ÿ’ป Author #

Okolo Miracle Echezona
๐Ÿ“ง okolomiracle513@gmail.com
๐ŸŒ GitHub | LinkedIn


1
likes
0
points
38
downloads

Publisher

unverified uploader

Weekly Downloads

A Dart package that allows JavaScript-like dot-access to deeply nested Maps and Lists.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, glob

More

Packages that depend on magic_map