File I/O
Part of the API Reference. Reads and writes files, guessing the format from the file extension unless format is supplied. Each has an Async counterpart with identical parameters, for callers that avoid synchronous file I/O.
pdataReadFile
dynamic pdataReadFile(String path, {PdataFormat? format})
Reads and decodes the file at path. Throws PdataFileNotFoundException if path doesn’t exist, and PdataFormatException if format isn’t given and can’t be guessed from path.
final config = pdataReadFile('config.yaml');
print(config['name']);
pdataReadFileAsync
Future<dynamic> pdataReadFileAsync(String path, {PdataFormat? format})
Async equivalent of pdataReadFile.
pdataWriteFile
void pdataWriteFile(
String path,
dynamic data, {
PdataFormat? format,
bool pretty = true,
bool createDirectories = true,
})
Encodes data and writes it to the file at path. When createDirectories is true (the default), any missing parent directories are created first. Throws PdataFormatException if format isn’t given and can’t be guessed from path, or if data can’t be represented in that format.
pdataWriteFile('config.yaml', {'name': 'my-app', 'debug': true});
pdataWriteFileAsync
Future<void> pdataWriteFileAsync(
String path,
dynamic data, {
PdataFormat? format,
bool pretty = true,
bool createDirectories = true,
})
Async equivalent of pdataWriteFile.
pdataFormatFromExtension
PdataFormat? pdataFormatFromExtension(String path)
Guesses a PdataFormat from path’s extension. Returns null if path has no extension, or one that isn’t .json, .yaml, .yml, or .toml. This is what every function above calls internally when format isn’t supplied.