Async
Part of the Examples. The Async counterparts of the file functions, including a few concurrent reads via Future.wait.
Source: example/async_example.dart
// Run this example from the package root:
// dart run example/async_example.dart
// print() is fine in example scripts; avoid it in production code instead.
// ignore_for_file: avoid_print
import 'package:pdata/pdata.dart';
Future<void> main() async {
final config = <String, dynamic>{
'name': 'my-app',
'workers': 4,
'endpoints': ['/health', '/status'],
};
// Async variants avoid blocking the event loop — handy in servers or
// any code that's already async elsewhere.
await pdataWriteFileAsync('build/async_config.toml', config);
final loaded =
await pdataReadFileAsync('build/async_config.toml') as Map<String, dynamic>;
print('Loaded ${loaded.length} top-level key(s) from TOML:');
loaded.forEach((key, value) => print(' $key: $value'));
// Kick off a few reads concurrently — each awaits independently.
await pdataWriteFileAsync('build/async_config.json', config);
await pdataWriteFileAsync('build/async_config.yaml', config);
final results = await Future.wait([
pdataReadFileAsync('build/async_config.json'),
pdataReadFileAsync('build/async_config.yaml'),
pdataReadFileAsync('build/async_config.toml'),
]);
print('\nAll three formats round-tripped equal: '
'${results.every((r) => r.toString() == results.first.toString())}');
}
Run it from the package root:
dart run example/async_example.dart