dart-io

dart:io sync vs async file operations

痞子三分冷 提交于 2021-02-19 09:04:46
问题 There are a number of sync and async operations for files in dart:io: file.deleteSync() and file.delete() file.readAsStringSync() and file.readAsString() file.writeAsBytesSync(bytes) and file.writeAsBytes(bytes) and many, many more. What are the considerations that I should keep in mind when choosing between the sync and async options? I seem to recall seeing somewhere that the sync option is faster if you have to wait for it to finish anyway ( await file.delete() for example). But I can't

How do I get the directory of the current script, in Dart?

和自甴很熟 提交于 2021-02-16 16:06:45
问题 I want to know what the script's directory is. I have a command-line Dart script. 回答1: If you're doing this for a console based app (e.g. in a unit test) and intend to use the output to open a file for reading or writing, it's more helpful to use Platform.script.path : import "package:path/path.dart" show dirname, join; import 'dart:io' show Platform; main() { print(join(dirname(Platform.script.path), 'test_data_file.dat'); } The result of that command can be used with a File object and be

How do I read console input / stdin in Dart?

依然范特西╮ 提交于 2020-12-28 06:59:32
问题 How do I read console input from stdin in Dart? Is there a scanf in Dart? 回答1: The readLineSync() method of stdin allows to capture a String from the console: import 'dart:io'; main() { print('1 + 1 = ...'); var line = stdin.readLineSync(encoding: Encoding.getByName('utf-8')); print(line.trim() == '2' ? 'Yup!' : 'Nope :('); } 回答2: The following should be the most up to date dart code to read input from stdin. import 'dart:async'; import 'dart:io'; import 'dart:convert'; void main() { readLine

How do I read console input / stdin in Dart?

杀马特。学长 韩版系。学妹 提交于 2020-12-28 06:52:33
问题 How do I read console input from stdin in Dart? Is there a scanf in Dart? 回答1: The readLineSync() method of stdin allows to capture a String from the console: import 'dart:io'; main() { print('1 + 1 = ...'); var line = stdin.readLineSync(encoding: Encoding.getByName('utf-8')); print(line.trim() == '2' ? 'Yup!' : 'Nope :('); } 回答2: The following should be the most up to date dart code to read input from stdin. import 'dart:async'; import 'dart:io'; import 'dart:convert'; void main() { readLine

How do I read console input / stdin in Dart?

旧时模样 提交于 2020-12-28 06:50:50
问题 How do I read console input from stdin in Dart? Is there a scanf in Dart? 回答1: The readLineSync() method of stdin allows to capture a String from the console: import 'dart:io'; main() { print('1 + 1 = ...'); var line = stdin.readLineSync(encoding: Encoding.getByName('utf-8')); print(line.trim() == '2' ? 'Yup!' : 'Nope :('); } 回答2: The following should be the most up to date dart code to read input from stdin. import 'dart:async'; import 'dart:io'; import 'dart:convert'; void main() { readLine

How To Serve Files With HttpServer In Dart

回眸只為那壹抹淺笑 提交于 2020-04-10 05:42:41
问题 I have the following standard structure for my Dart app: / pubspec.yaml web/ main.css main.dart main.html build.dart server.dart When I receive GET requests on the server, I want the server to use the web directory as root and serve the content of the files to clients. How can I do this? This is how far I've gotten so far: import 'dart:io'; void main() { HttpServer.bind('127.0.0.1', 80).then((HttpServer server) { server.listen((request) { print("got request"); switch (request.method) { case

Clearing the terminal screen in a command-line Dart app

為{幸葍}努か 提交于 2020-01-13 18:15:29
问题 This one don't work (on Windows in a Cmd-Box): import 'dart:io'; void main() { print("Hello, World!"); Process.start('cls', [], runInShell: true).then((process) { stdout.addStream(process.stdout); stderr.addStream(process.stderr); }); } 回答1: EDIT This seems to have the answer why it doesn't work on windows How to make win32 console recognize ANSI/VT100 escape sequences? ORIGINAL if(Platform.isWindows) { // not tested, I don't have Windows // may not to work because 'cls' is an internal

How can I do stdin.close() with the new Streams API in Dart?

 ̄綄美尐妖づ 提交于 2020-01-05 07:41:30
问题 I ask user for input in my command line (dart:io) app. After I get my answer from the user, I want to unsubscribe from the Stream . Then, later, I may want to listen to it again (but with different listener, so pause() and resume() don't help me). On startup, I have this: cmdLine = stdin .transform(new StringDecoder()); Later, when I want to gather input: cmdLineSubscription = cmdLine.listen((String line) { try { int optionNumber = int.parse(line); if (optionNumber >= 1 && optionNumber <=

Dart basic auth not working (dart:io) (UPDATED to working code)

北城余情 提交于 2020-01-05 04:43:07
问题 I'm working with the Harvest API, a pretty standard web service API, and my curl requests are working just fine while my Dart HttpClient requests are not. Here is my curl request (with sensitive information disguised, of course): curl -H "Content-Type: application/json" \ -H "Accept: application/json" \ -u "address@domain.com:password" \ https://my_domain.harvestapp.com/account/who_am_i UPDATE --- The following code now works: HttpClient client = new HttpClient(); client.addCredentials( Uri

How to set the current working directory in Dart? [duplicate]

六月ゝ 毕业季﹏ 提交于 2019-12-31 00:30:36
问题 This question already has an answer here : How do I find the current directory, in Dart? (1 answer) Closed 2 years ago . In a dart console application, how do I set the current working directory? 回答1: import 'dart:io'; ... Directory.current = new Directory('your/path/here'); or just Directory.current = 'your/path/here'; See also https://api.dartlang.org/133671/dart-io/Directory/current.html 来源: https://stackoverflow.com/questions/33020117/how-to-set-the-current-working-directory-in-dart