I have a Dart code that need to take a path and work on it. For portability purposes I need the path to be relative and have Dart convert it to absolute path. How can I do this.
For example:
var samplePath = 'relative/path/file.txt';
// converted to 'C:/Users/XYZ/Desktop/relative/path/file.txt'
Flutter 2.7
import 'package:path/path.dart' as p;
var absPath = p.absolute('a/b', 'c');
// '...<current working dir>/a/b/c
Documentation
I have never programmed in Dart, but in this part of the documentation there is a property of the class File which does what you want.
Moreover, if you have the root path of your relative path (maybe your current directory) you can join them using the join function
Hope it helps you.
Related
I only get one uri from the file_picker when saving. But so that I can display the correct path, I need the absolute path of the file. Unfortunately I haven't found a working solution. The library flutter_absolute_path unfortunately doesn't seem to work with the new Android versions anymore.
Does anyone have any idea how I could get the absolute path of the file.
example:
content://com.android.providers.downloads.documents/document/297
convert to:
/data/user/0/xxx/cache/dummy1.png
Check out the toFilePath() method of the dart:core package to see if it solves your problem.
I thought so too. But unfortunately I get an exception.
test code:
//uri output from file picker (fileInfo.identifier)
var uriPath = "content://com.android.providers.downloads.documents/document/297";
var uri = Uri.parse(uriPath);
print(uri.toFilePath());
exception
-> Unsupported operation: Cannot extract a file path from a content URI
In flutter it's easy to load a .txt asset at runtime by specifying it or its folder in the pubspec.yaml file and then loading it with rootBundle. However, i'm working on a pure dart package, and I'm struggling to work out how to get the package to load a .txt file relative to it's own directory structure.
When I use the package in a separate dart command line application i'm working on, the relative path that I specified in one of the package source code files causes an error to be thrown that the txt file doesn't exist. I understand why this error is being thrown, because the relative path is interpreted as being from the command line application's root directory instead of the package's root directory, but i'm unsure of how to solve this without specifying the absolute path for the .txt file. I'd rather not specify the absolute path as it makes the package less portable.
Is there anything similar to flutter's asset loading for a pure dart package?
I think you need the resolveSymbolicLinks or resolveSymbolicLinksSync methods to decode the relative path and then use the resolved path to read the txt file:
import 'dart:io';
void main() async {
String file = '../lib/main.dart';
var path = Uri.parse('.').resolveUri(Uri.file(file)).toFilePath();
print(path);
if (path == '') path = '.';
var resolved = await File(path).resolveSymbolicLinks();
print(resolved);
File(resolved).readAsString().then((String contents) {
print(contents);
});
}
I want to know what the current directory is. I don't want to shell out to run pwd. Is there an easy way to do this in Dart?
Indeed there is!
import 'dart:io';
main() {
Directory current = Directory.current;
}
Note: this only works on the command-line, not in the browser.
Read more about the API docs for Directory.current.
Directory.current.path does it if you want a string, Directory.current for a Directory.
(note: Directory is defined in dart:io)
I have string variable which represents the full path of some file, like:
x = "/home/user/.local/share/app/some_file" on Linux
or
x = "C:\\Program Files\\app\\some_file" on Windows
I'm wondering if there is some programmatic way, better then splitting string manually to get to directory path
How do I return directory path (path without filename) in Lua, without loading additional library like LFS, as I'm using Lua extension from other application?
In plain Lua, there is no better way. Lua has nothing working on paths. You'll have to use pattern matching. This is all in the line of the mentality of offering tools to do much, but refusing to include functions that can be replaced with one-liners:
-- onelined version ;)
-- getPath=function(str,sep)sep=sep or'/'return str:match("(.*"..sep..")")end
getPath=function(str,sep)
sep=sep or'/'
return str:match("(.*"..sep..")")
end
x = "/home/user/.local/share/app/some_file"
y = "C:\\Program Files\\app\\some_file"
print(getPath(x))
print(getPath(y,"\\"))
Here is a platform independent and simpler solution based on jpjacobs solution:
function getPath(str)
return str:match("(.*[/\\])")
end
x = "/home/user/.local/share/app/some_file"
y = "C:\\Program Files\\app\\some_file"
print(getPath(x)) -- prints: /home/user/.local/share/app/
print(getPath(y)) -- prints: C:\Program Files\app\
For something like this, you can just write your own code. But there are also libraries in pure Lua that do this, like lua-path or Penlight.
How do I obtain the fully qualified path of an isolated storage file for a WPF application?
You can use reflection to do so, as shown in the linked forum post:
IsolatedStorageFileStream oStream =
new IsolatedStorageFileStream(ISOLATED_FILE_NAME, FileMode.Create, isoStore);
// Get the physical path using reflection
String filePath = oStream.GetType().GetField("m_FullPath",
BindingFlags.Instance | BindingFlags.NonPublic).GetValue(oStream).ToString();
Console.WriteLine(filePath);
On Windows 10 Mobile the isolated-storage-path is equal to Windows.Storage.ApplicationData.Current.LocalFolder.
If you know the relative file-path inside the isolated-storage you can use System.IO.Path.Combine() to create the full path.
You can use IsolatedStorageFile.GetUserStoreForApplication().GetFileNames() to list all files in the isolated-storage.