getPackageDirectory function

Future<Directory?> getPackageDirectory({
  1. String package_name = "package:general_lib/general_lib.dart",
})

Implementation

Future<Directory?> getPackageDirectory({
  String package_name = "package:general_lib/general_lib.dart",
}) async {
  Uri? res = await Isolate.resolvePackageUri(Uri.parse(package_name));
  if (res == null) {
    return null;
  }

  List<String> paths = [...res.pathSegments];
  for (var i = 0; i < package_name.split("/").length; i++) {
    paths.removeLast();
  }
  Directory directory = Directory(joinAll(paths));

  if (!directory.existsSync()) {
    directory = Directory(joinAll(["/", ...paths]));
  }
  return directory;
}