How do I find the current directory, in Dart? - dart

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)

Related

Is there any way of loading local resource files in dart? (not flutter)

I want to load the bytes of a file into a variable while testing my flutter application.
I can't use the assets directory as those are bundled with the app and require WidgetsFlutterBinding.ensureInitialized();
I tried searching the file manually with the path package, but this did not seem to work and was rather hacky. That is why i'm searching for a more official approach.
I was thinking way to complicated ...
As Chuck Batson commented, you can just use the path from the projects root for passing it into the (dart:io) File:
File loadResource(String relativePath) {
final filePath = path.join("test", "resources", relativePath);
return File(filePath);
}
(Notice: The above code makes use of the path package for constructing a file path.)

How to read and write a text file in Flutter

How do you read text from a file and write text to a file?
I've been learning about how to read and write text to and from a file. I found another question about reading from assets, but that is not the same. I will add my answer below from what I learned from the documentation.
Setup
Add the following plugin in pubspec.yaml:
dependencies:
path_provider: ^1.6.27
Update the version number to whatever is current.
And import it in your code.
import 'package:path_provider/path_provider.dart';
You also have to import dart:io to use the File class.
import 'dart:io';
Writing to a text file
_write(String text) async {
final Directory directory = await getApplicationDocumentsDirectory();
final File file = File('${directory.path}/my_file.txt');
await file.writeAsString(text);
}
Reading from a text file
Future<String> _read() async {
String text;
try {
final Directory directory = await getApplicationDocumentsDirectory();
final File file = File('${directory.path}/my_file.txt');
text = await file.readAsString();
} catch (e) {
print("Couldn't read file");
}
return text;
}
Notes
You can also get the path string with join(directory.path, 'my_file.txt') but you need to import 'package:path/path.dart'.
Flutter's Official Documentation of Reading and Writing Files
This works for iOS, Android, Linux and MacOS but not for web.
As additional info to #Suragch's answer, if you want to find the file you created, you can do as the images show:
And then inside that data folder, go again to a folder named data and search for your package, and then go to:
If you happen to create new files, in order to be able to see them, just right click and click Synchronize.
An another way to pull the file from the device is by using adb pull command. You can find the file path by debugging the code and then use adb pull command. adb is located in Android SDK -> platform-tools directory.
./adb pull /storage/emulated/0/Android/data/com.innovate.storage.storage_sample/files/sample.txt ~/Downloads
#Suragch 's answer is right. Except the version of path_provider that you want to use now is:
path_provider: ^2.0.9

Dart accessing the name of a File or Directory

How do I read the name of a File or a Directory?
There is a property 'path' but that returns the entire file path.
Would be nice to have a property like 'name' that just returns the last part of the path.
In Java there is a method called File.name();
You can use the path package to do that :
import 'package:path/path.dart' as path;
main() {
path.basename('path/to/foo.dart'); // -> 'foo.dart'
path.basename('path/to'); // -> 'to'
}
See the path package documentation for more explanations.
Since Dart Version 2.6 has been announced and it's available for flutter version 1.12 and higher, You can use extension methods. It will provide a more readable and global solution to this problem.
file_extensions.dart :
import 'dart:io';
extension FileExtention on FileSystemEntity{
String get name {
return this?.path?.split("/")?.last;
}
}
and name getter is added to all the file objects. You can simply just call name on any file.
main() {
File file = new File("/dev/dart/work/hello/app.dart");
print(file.name);
}
Read the document for more information.
Note:
Since extension is a new feature, it's not fully integrated into IDEs yet and it may not be recognized automatically. You have to import your extension manually wherever you need that. Just make sure the extension file is imported:
import 'package:<your_extention_path>/file_extentions.dart';

How to get Dart's Web UI to put an image in the out folder

Dart's Web UI performs a compile step which puts the generated files into an "out" folder. I can't figure out how to get an image to be placed in that out folder, though. Does anyone know how?
I have the images in the web folder in a folder called img, although I've also tried putting them directly in web. Should I create a top level folder named resources and put them in there instead?
I had the same problem, and have almost solved it by updating my build.dart to include a copy of the img folder into the out folder.
import 'dart:io';
import 'dart:async';
import 'package:web_ui/component_build.dart';
// Ref: http://www.dartlang.org/articles/dart-web-components/tools.html
// Actually ... https://github.com/sethladd/dart-web-components-tests/blob/master/build.dart
main() {
var args = new List.from(new Options().arguments);
// args.addAll(['--', '--no-rewrite-urls']);
Future dwc = build(args, ['web/index.html']);
dwc
.then((_) => Process.run('cp', ['-fR', 'web/img', 'web/out']));
}
In the above I've added a command to run "cp" (I'm on a Mac) of web/img to web/out.
I say almost solved it because those image in the img folder also end up being copied directly into the out folder as well as out/img, which isn't ideal but doesn't harm anything for me just now. I believe these extra copies are from build.dart being triggered by the copy, haven't found a way to stop this yet.

Recursively remove entire directory with files in Dart?

How do you recursively remove an entire directory in Dart along with all the files?
For example:
/path/to/project/foo.dart
/path/to/project/remove/all/of/these
It's easier than it sounds.
If I understood you right, it would go like this:
import 'dart:io';
main() {
// Deletes the directory "remove" with all folders and files under it.
new Directory('remove').delete(recursive: true);
}

Resources