To safely and easily retrieve values from a map/json with useful and human friendly error messages. With little to no effort you can parse and guarantee the values type.
Retriever has 2 main features when parsing values from a map:
// This method will throw an error if the value is invalid
Retriever.getString('key', map);
// This method will return null if the value is invalid
Retriever.getMaybeString('key', map);
RetrieverFormatError: Found invalid format when parsing key.
Key: "name"
Expected: "Integer"
Found: "String", value: "myExampleName".
Object: "{
--> name: myExampleName
^^^^
age: 40
}"
With retriever you can safely parse:
- String
- Integer
- Double
- Date
- Map
Retriever has no dependencies other than dart it self, and all methods are static. This makes easy and performant to use.
Giving the following map.
final map = {
'name': 'myExampleName',
'age': 40,
};
You can retrieve a string from a Map, just like that.
final string = Retriever.getString('name', map);
print(string); // 'myExampleName'
If the key doesn't exist or the value is not a valid, an error will be thrown. The errors are very helpful and human readable, showing exactly what's going on.
// Using getString on a "int" value will throw the following error
Retriever.getString('age', map); // 'age' is a int. error will be thrown
// RetrieverFormatError: Found invalid format when parsing key.
// Key: "name"
// Expected: "Integer"
// Found: "String", value: "myExampleName".
// Object: "{
// --> name: myExampleName
// ^^^^
// age: 40
// }"
Feel free to file an issue if you find a problem or make pull requests.
All contributions are welcome :)