uriForPackage static method
Returns the uri for a package
name and a libPath
that contains the wasm module.
The wasm module file should be in lib/libPath
.
The envVariable
is used to get the path to the wasm module
from the environment variables.
For compiled native (non-web) Flutter you should use Flutter assets or an HTTP endpoint, such as from a Github release.
Implementation
static Future<Uri> uriForPackage({
required String package,
required String libPath,
required String? envVariable,
}) async {
const isWeb = identical(0, 0.0);
if (isWeb) {
return Uri.parse(
kIsFlutter
? 'asset:packages/$package/lib/$libPath'
: './packages/$package/$libPath',
);
} else {
final envFile = Platform.environment[envVariable];
final scriptRoot = File.fromUri(Platform.script).parent.parent;
Uri? packageUri;
try {
packageUri = await Isolate.resolvePackageUri(
Uri.parse('package:$package/$libPath'),
);
} catch (_) {}
final options = [
if (envFile != null) Uri.parse(envFile),
// dart run package:script
scriptRoot.uri.resolve('lib/$libPath'),
// dart run test
Directory.current.uri.resolve('lib/$libPath'),
// another package
if (packageUri != null) packageUri,
// some_dir/script.exe
Platform.script.resolve(libPath),
];
final wasmFile = options.firstWhere(
(option) => File.fromUri(option).existsSync(),
orElse: () => globalLoadAsset != null
? Uri.parse('asset:packages/$package/lib/$libPath')
: options.first,
);
return wasmFile;
}
}