directoryPath property

String get directoryPath

Implementation

String get directoryPath {
  try {
    final stackTraceString = _stackTraceFetcher.currentStackTrace;
    final uriRegExp =
        RegExp(isWindows ? _windowsPattern : _linuxMacOSPattern);
    final match = uriRegExp.firstMatch(stackTraceString);

    if (match != null) {
      String filePath;
      if (isWindows) {
        final rawPath = match.group(1)!;
        filePath = Uri.file(rawPath, windows: true).toFilePath(windows: true);
      } else {
        filePath = Uri.tryParse('file:///${match.group(1)!}')!.toFilePath();
      }

      final separator = isWindows ? '\\' : '/';
      final directoryPath =
          filePath.substring(0, filePath.lastIndexOf(separator));
      return directoryPath;
    } else {
      throw FileNotFoundException(
        message: 'File not found in stack trace',
        stackTrace: StackTrace.current,
      );
    }
  } catch (e) {
    rethrow;
  }
}