You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've solved this elsewhere using a relativeTo parameter:
/// Returns a [Uri] for this path that can be used in a Dart import statement.UritoDartUri({Uri relativeTo}) {
if (relativeTo !=null) {
// Attempt to construct relative import.Uri normalizedBase = relativeTo.normalizePath();
List<String> baseSegments = pkg_path.split(normalizedBase.path)
..removeLast();
List<String> targetSegments = pkg_path.split(toAbsoluteUri().path);
if (baseSegments.first == targetSegments.first &&
baseSegments[1] == targetSegments[1]) {
// Ok, we're in the same package and in the same top-level directory.String relativePath = pkg_path.relative(
targetSegments.skip(2).join('/'),
from: baseSegments.skip(2).join('/'));
returnnewUri(path: relativePath);
}
}
var pathSegments = pkg_path.split(path);
if (pathSegments.first !='lib') {
thrownewStateError(
'Cannot construct absolute import URI from ${relativeTo} ''to a non-lib Dart file: ${toAbsoluteUri()}');
}
var packagePath = pkg_path.joinAll(pathSegments.sublist(1));
returnnewUri(
scheme: isDartSdk ? _dartPackage :'package',
path: isDartSdk ? path :'$package/$packagePath');
}
The text was updated successfully, but these errors were encountered:
As of right now, invalid code is produced when referring to files outside of
lib/
:In
foo_test.g.dart
, an import is added like:It should instead be:
I've solved this elsewhere using a
relativeTo
parameter:The text was updated successfully, but these errors were encountered: