Gato is a dart utility library inspired by javascript lodash library.
Show some ❤️ and star the repo to support the project
1. Depend on it
Add gato: ^0.0.5
to your pubspec.yaml
dependencies:
dependencies:
gato: ^0.0.5
2. Import it
Now in your Dart code, you can use:
import 'package:gato/gato.dart' as gato;
Get value from a Map
by path. Use dot notation in [path] to access nessted keys.
gato.get<T>(Map<String, dynamic> map, String path, {T Function(dynamic)? converter})
Arguments
- map (
Map<String, dynamic>
): The map you want to get value from. - path (
String
): The path of the property to get. - converter (
T Function(dynamic)?
): The function to cast the value to a custom type
Returns
<T>|null
: Returns the resolved value ornull
if thepath
is not found.
Example
Map map = {'a': {'b': 1}, 'c': '0xFFB74093'};
var b = gato.get(map, 'a.b');
var b = gato.get<int>(map, 'a.b');
Color color = gato.get<Color>(map, 'c', converter: (value) => Color(int.parse(value)));
Sets the value at path of map. If a portion of path doesn't exist, it's created.
Map<String, dynamic> set<T>(Map<String, dynamic> map, String path, T value)
Arguments
- map (
Map<String, dynamic>
): The map to modify. - path (
String
): The path of the property to set. - value (
T
): The value to set.
Returns
Map<String, dynamic>
: Returns updated map.
Example
Map map = {'a': {'b': 1}};
map = gato.set(map, 'a.b', 2);
or
map = gato.set<int>(map, 'a.b', 2);
Removes the property at path of map.
Map<String, dynamic> unset(Map<String, dynamic> map, String path)
Arguments
- map (
Map<String, dynamic>
): The map to modify. - path (
String
): The path of the property to remove.
Returns
Map<String, dynamic>
: Returns updated map.
Example
Map map = {'a': {'b': 1}, 'c': 2};
map = gato.set(map, 'c'); // {'a': {'b': 1}}