gpt4 book ai didi

flutter - 在 Flutter 测试中从文件中读取资源

转载 作者:行者123 更新时间:2023-12-05 02:58:49 25 4
gpt4 key购买 nike

我想从 Flutter 单元测试中读取包含一些测试数据的文件。有推荐的方法吗?我尝试了 resource包,但这会引发错误:

dart:isolate                              Isolate.resolvePackageUri
package:resource/src/resolve.dart 11:20 resolveUri
package:resource/src/resource.dart 74:21 Resource.readAsString

Unsupported operation: Isolate.resolvePackageUri

最佳答案

最后我使用了以下技巧(唉,我不记得我在哪里看到它建议正确归属它)来扫描文件系统树以找到项目根目录。这样,无论测试从哪个目录执行,测试都可以找到数据(否则我的测试在命令行上通过,但在 Android Studio 中失败)。

/// Get a stable path to a test resource by scanning up to the project root.
Future<File> getProjectFile(String path) async {
var dir = Directory.current;
while (!await dir.list().any((entity) => entity.path.endsWith('pubspec.yaml'))) {
dir = dir.parent;
}
return File('${dir.path}/$path');
}

关于flutter - 在 Flutter 测试中从文件中读取资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58592859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com