Http POST request with json content-type in dart:io

This is a complete example. You have to use json.encode(…) to convert the body of your request to JSON. import ‘package:http/http.dart’ as http; import ‘dart:convert’; import ‘dart:io’; var url = “https://someurl/here”; var body = json.encode({“foo”: “bar”}); Map<String,String> headers = { ‘Content-type’ : ‘application/json’, ‘Accept’: ‘application/json’, }; final response = http.post(url, body: body, headers: headers); final … Read more

How do I create a file in a directory structure that does not exist yet in Dart?

Alternatively: new File(‘path/to/file’).create(recursive: true); Or: new File(‘path/to/file’).create(recursive: true) .then((File file) { // Stuff to do after file has been created… }); Recursive means that if the file or path doesn’t exist, then it will be created. See: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-io.File#id_create EDIT: This way new Directory doesn’t need to be called! You can also do this in a … Read more

How do I read console input / stdin in Dart?

The readLineSync() method of stdin allows to capture a String from the console: import ‘dart:convert’; import ‘dart:io’; void main() { print(‘1 + 1 = …’); var line = stdin.readLineSync(encoding: utf8); print(line?.trim() == ‘2’ ? ‘Yup!’ : ‘Nope :(‘); } Old version: import ‘dart:io’; main() { print(‘1 + 1 = …’); var line = stdin.readLineSync(encoding: Encoding.getByName(‘utf-8’)); … Read more

How do I list the contents of a directory with Dart?

How to list the contents of a directory in Dart final dir = Directory(‘path/to/directory’); final List<FileSystemEntity> entities = await dir.list().toList(); This creates a Directory from a path. Then it converts it to a list of FileSystemEntity, which can be a File, a Directory, or a Link. By default subdirectories are not listed recursively. If you … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)