An update to the resource package has been published.
Update This is being recycled. The Resource class will be moved to the package.
Original
Dart has a new Resource class (tested with Dart VM version: 1.12.0-edge.d4d89d6f12b44514336f1af748e466607bcd1453 (Fri Aug 7 19:38:14 2015) )
resource_example / Library / resources / sample.txt
Sample text file.
resource_example / bin / main.dart
main() async { var resource = const Resource('package:resource_example/resource/sample.txt'); print(await resource.readAsString()); // or for binary resources // print(await resource.readAsBytes()); }
Performance
dart bin/main.dart
prints
Sample text file.
Performance
pub global activate -spath . pub global run resource_example:main
also prints
Sample text file.
It is also possible to pass the contents of a variable as a resource. This may, for example, be convenient in unit tests for transferring mock resources.
const sampleText = "Sample const text"; main() async { var uriEncoded = sampleText.replaceAll(' ', '%20'); var resource = new Resource('data:application/dart;charset=utf-8,$uriEncoded'); print(await resource.readAsString()); }
Arbitrary files or http uris are also supported, but this may not work when using pub run or pub global run when using relative paths.
main() async { var uri = new Uri.file('lib/resource/sample.txt'); var resource = new Resource(uri.toString()); print(await resource.readAsString()); }
See details
- http://blog.sethladd.com/2015/08/dynamically-load-package-contents-with.html
- https://codereview.chromium.org/1276263002/
source share