Getting Started
pdata requires Dart SDK ^3.5.0.
1. Install the package
dart pub add pdata
Or add it to pubspec.yaml directly, then run dart pub get:
dependencies:
pdata: ^1.0.0
2. Create a config file
Any of the three formats works — here’s YAML:
name: my-app
debug: false
port: 8080
tags:
- cli
- tool
3. Read it
import 'package:pdata/pdata.dart';
void main() {
final config = pdataReadFile('config.yaml') as Map<String, dynamic>;
print(config['name']);
}
pdataReadFile guesses the format from the extension (.json, .yaml/.yml, or .toml) and returns a Map<String, dynamic> (or a List for a top-level JSON/YAML array). Index into it directly, or use the typed accessors for anything that isn’t already the right type:
final port = config.getInt('port', defaultValue: 8080);
4. Write it back
pdataWriteFile('config.json', config); // as JSON
pdataWriteFile('config.toml', config); // as TOML
pdataWriteFile('config.yaml', config); // as YAML
The format is guessed from the destination path the same way, so converting between formats is just reading one path and writing another.
5. Handle a missing file
try {
final config = pdataReadFile('config.yaml');
} on PdataFileNotFoundException catch (e) {
print('${e.path} does not exist.');
}
6. Run it
dart run bin/my_app.dart
Next steps
- See Supported Formats for exactly what pdata’s YAML and TOML parsers accept.
- Browse the Examples for typed config, error handling, and async loading.
- Look up every parameter in the API Reference.