SettingsYaml class
Provides the ability to read/write simply settings stored in a yaml file without having to worry about parsing the yaml file or programatically traversing the yaml tree.
password: xxxx
user: xxxx
'''
To obtain the value of password use:
```dart
final settings = SettingsYaml.load(pathToSettings: 'path to yaml.yaml');
final password = settings.asString['password'];
You can also update an entry:
settings['password'] = 'abc123';
settings.save();
You can also read attributes stored in a classic yaml hierachy using selectors similar to html xpath selectors.
A selector path is made of words seperated by periods. e.g. environment.sdk
You can also craft a selector to access a specific element in a yaml list using the '[]' operators.
To access a list use 'wordn
' where 'n' is the nth instance of the word
in a yaml array.
e.g.
one:
- two
- two
three:
four: value
To return the value of four
traverse('one.two[1].three.four') == 'value'
Constructors
- SettingsYaml.fromString({required String content, required String filePath})
-
Loads settings from a string.
The
content
must be formatted like a standard yaml file would be: - SettingsYaml.load({required String pathToSettings})
-
Loads a settings file from the give
pathToSettings
.factory
Properties
Methods
-
asBool(
String path, {bool defaultValue = true}) → bool -
returns the value at
path
as an bool. If the value isn't an bool then an exception will be thrown. If the key isn't a valid bool thendefaultValue
is returned Use validBool to determine if the key exists and is a valid bool. -
asDouble(
String path, {double defaultValue = 0.0}) → double -
returns the value at
path
as an double. If the value isn't an double then an exception will be thrown. If the key isn't a valid int thendefaultValue
is returned. Use validDouble to determine if the key exists and is a valid double. -
asInt(
String path, {int defaultValue = 0}) → int -
returns the value at
path
as an int. If the value isn't an int then an exception will be thrown. If the key isn't a valid int thendefaultValue
is returned Use validInt to determine if the key exists and is a valid int. -
asString(
String path, {String defaultValue = ''}) → String -
returns the value at
path
as an String. If the value isn't an String then an exception will be thrown. If the key isn't a valid String thendefaultValue
is returned Use validString to determine if the key exists and is a valid String. -
asStringList(
String path, {List< String> defaultValue = const <String>[]}) → List<String> -
Returns a list at
path
as a String list. If the key isn't a valid List -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
save(
) → Future< void> - Saves the settings back to the settings file. To avoid a corrupted file in the event of a crash we first copy the existing settings file to a .bak file. If the save fails you may need to manually rename the .bak file.
-
selectAsBool(
String selector) → bool? -
Returns the boolean attribute at
selector
See traverse for details on the syntax ofselector
Throws a SettingsYamlException if an error occurs reading the selector Throws a PathNotFoundException exception If the selector doesn't lead to a valid location. -
selectAsDouble(
String selector) → double? -
Returns the double attribute at
selector
See traverse for details on the syntax ofselector
Throws a SettingsYamlException if an error occurs reading the selector Throws a PathNotFoundException exception If the selector doesn't lead to a valid location. -
selectAsInt(
String selector) → int? -
Returns the int attribute at
selector
See traverse for details on the syntax ofselector
Throws a SettingsYamlException if an error occurs reading the selector Throws a PathNotFoundException exception If the selector doesn't lead to a valid location. -
selectAsList(
String selector) → List? -
Returns the list at
selector
See traverse for details on the syntax ofselector
Throws a SettingsYamlException if an error occurs reading the selector Throws a PathNotFoundException exception If the selector doesn't lead to a valid location. -
selectAsMap(
String selector) → Map< String, dynamic> ? -
Returns the map at
selector
See traverse for details on the syntax ofselector
Throws a SettingsYamlException if an error occurs reading the selector Throws a PathNotFoundException exception If the selector doesn't lead to a valid location. -
selectAsString(
String selector) → String? -
Returns the String attribute at
selector
See traverse for details on the syntax ofselector
Throws a SettingsYamlException if an error occurs reading the selector Throws a PathNotFoundException exception If the selector doesn't lead to a valid location. -
selectorExists(
String selector) → bool -
Returns true if the given
selector
exists in the settings file. -
toString(
) → String -
A string representation of this object.
inherited
-
traverse(
String selector) → dynamic -
validBool(
String key) → bool - Returns true if the key has a value which is a bool. Empty or null value returns false.
-
validDouble(
String key) → bool - Returns true if the key has a value which is a double. Empty or null value returns false.
-
validInt(
String key) → bool - Returns true if the key has a value which is an int. Empty or null value returns false.
-
validString(
String key) → bool - Returns true if the key has a value which is a String which is non-null and not empty
-
validStringList(
String key) → bool
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator [](
String path) → dynamic - Returns the value for the given key.
-
operator []=(
String path, dynamic value) → void - Adds or Updates the given key/value pair.