如何在Dart中加载资源文件或包:URI? [英] How to load resource files or package: URIs in Dart?

查看:380
本文介绍了如何在Dart中加载资源文件或包:URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以获取当前执行的脚本的路径,然后找到相对于此位置的资源文件,但不能保证在发布应用程序时目录结构相同。

$ Dart提供了一种通用的方式来加载资源(文件,数据,...)以一种方式工作也与 pub run pub全局运行



另一种方式问我如何动态加载<$ c $

解决方案

更新
已发布资源包。



更新
正在重做。 资源类将移至包。



原始

在Dart中有一个新的 Resource 类(测试使用 Dart VM版本:1.12.0-edge .d4d89d6f12b44514336f1af748e466607bcd1453(Fri Aug 7 19:38:14 2015)



resource_example / lib / resource / sample.txt

 示例文本文件。 



resource_example / bin / main.dart b

  main()async {
var resource = const Resource('package:resource_example / resource / sample.txt');
print(await resource.readAsString());
//或二进制资源
// print(await resource.readAsBytes());
}

执行

  dart bin / main.dart 

print

 示例文本文件。 

执行

  pub global activate -spath。 
pub全局运行resource_example:main

也会输出

 示例文本文件。 

还可以将变量的内容作为资源传递。这可能例如在单元测试中方便地传递模拟资源。

  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());
}

支持任意文件或http uris但是在使用相对路径时使用 pub run pub全局运行时可能无效。

  main()async {
var uri = new Uri.file('lib / resource / sample.txt');
var resource = new Resource(uri.toString());
print(await resource.readAsString());
}

有关详情,请参阅

- http://blog.sethladd.com/2015/08/dynamically-load -package-contents-with.html

- https:// codereview。 chromium.org/1276263002/


There is a way to get the path of the currently executing script and then find the resource files relative to this location, but there is no guarantee that the directory structure is the same when the application is published.

Does Dart provide a generic way to load resources (files, data, ...) in a way that works also with pub run or pub global run?

Asked another way, how do I dynamically load the contents of a package: URI, in a Dart app, at runtime?

解决方案

Update The resource package was published.

Update This is being reworked. The Resource class will be moved to a package.

Original

There is a new Resource class in Dart (tested with Dart VM version: 1.12.0-edge.d4d89d6f12b44514336f1af748e466607bcd1453 (Fri Aug 7 19:38:14 2015))

resource_example/lib/resource/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());
}

Executing

dart bin/main.dart

prints

Sample text file.

Executing

pub global activate -spath . 
pub global run resource_example:main

also prints

Sample text file.

It's also possible to pass the content of a variable as a resource. This might for example be convenient in unit tests to pass 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 file or http uris are supported as well but this might 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());
}

For more details see
- http://blog.sethladd.com/2015/08/dynamically-load-package-contents-with.html
- https://codereview.chromium.org/1276263002/

这篇关于如何在Dart中加载资源文件或包:URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆