New build options for web ui on Dart 1.0 - dart

I have a Project using still web-ui, and trying to run it on Dart 1.0 but I get an error in this line of code:
build(new Options().arguments, ['web/lista_orden_p.html']);
What is the new syntax for this?

The Options class has been depreciated and removed from the dart:io library. the main() function now takes a List of strings as the arguments as such:
// build.dart
void main(List<String> args) {
build(args, ['web/lista_orden_p.html']);
}

Related

Dart How to load file in runtime

I'm writing a discord bot using the nyxx library and want use dynamic file import for load command info and handler. But, after 5 hours of searching with Google, I didn't find anything to help me do that.
In Node.js, I can use require() or import() for it: Does the dart have something like that?
A small code snippet, showing what I want do:
this.commands = new Collection();
fs.readdirSync('./src/commands').filter(( f ) => f.endsWith( '.js' )).forEach((file) => {
const command = require(`../commands/${file}`);
this.commands.set( command.info.name, command );
});
Is it possible to do this or not? (I don't like to write many imports for commands and register it in lib.)
You can in theory use Isolate.spawnUri to spawn external Dart programs to run in its own Isolate instances that can then communicate back to the main program using SendPort.
It does, however, come with some limitations. E.g. it is very limited what types of objects you can send though SendPort when using spawnUri since the two programs does not share any type information (compared to Isolate.spawn which does allow you to send your own custom types). The documented types you can send can be found here:
Null
bool
int
double
String
List or Map (whose elements are any of these)
TransferableTypedData
SendPort
Capability
https://api.dart.dev/stable/2.17.6/dart-isolate/SendPort/send.html
But it does allow us to make some kind of protocol and you can create some helper class around this to handle the conversion of a known object structure into e.g. Map<String, Object>.
A small example that works with Dart VM would be:
Your command implemented as: command.dart
import 'dart:isolate';
void main(List<String> arguments, Map<String, Object> message) {
final userName = message['username'] as String;
final sendPort = message['port'] as SendPort;
sendPort.send('Hi $userName. '
'You got a message from my external command program!');
}
Your server that calls your command: server.dart
import 'dart:isolate';
void main() {
final recievePort = ReceivePort();
recievePort.listen((message) {
print('Got the following message: $message');
recievePort.close();
});
Isolate.spawnUri(Uri.file('command.dart'), [], {
'username': 'julemand101',
'port': recievePort.sendPort,
});
}
If running this with: dart server.dart you, hopefully, get:
Got the following message: Hi julemand101. You got a message from my external command program!
If you want to compile your application, you can do so by doing the following. You need to compile the command.dart, since a compiled Dart program does not understand how to read Dart code.
dart compile exe server.dart
dart compile aot-snapshot command.dart
You should here change Uri.file('command.dart') to Uri.file('command.aot') since the file-extension for aot-snapshot are .aot.
If everything works, you should be able to see:
> .\server.exe
Got the following message: Hi julemand101. You got a message from my external command program!

Error in using Stdin.readLineSync(); in dart

in vs code, i am getting this error in the basic input taking code from the user
my complete code:
import 'dart:io';
void main(){
stdout.write("Enter your name : ");
var name = stdin.readLineSync();
stdout.write(name);
}
Error in the compiler:
playground.dart:9:23: Error: Method not found: 'Stdin.readLineSync'.
String name = Stdin.readLineSync();
^^^^^^^^^^^^
You must add:
import 'dart:io';
before your main function
To learn dart as console application you should use IntelliJ IDEA IDE.
This is the best IDE for dart.
vscode, dartpad does not support stdin stdout.
if you were getting error like
"Getter not found.'stdin'"
in VSCode check for the extension "dart" is installed on your VSCode then checkitout for run i.e dart run command
stdin.readLineSync()!
use the '!' as I did below:
void main(){
stdout.write("Enter your name : ");
var name = stdin.readLineSync()!;
stdout.write(name);
}
The error you get with stdin.readLineSync() is due to null safety introduced in Dart 2.12. Just add (!)
var name = stdin.readLineSync()!;
you should write it -> stdout.writeln and also import the library for it, I amended the code for you below, and it works fine on VSCode
import 'dart:io';
void main(){
stdout.writeln("Enter your name : ");
var name = stdin.readLineSync();
stdout.write(name);
}

Why does my script has all red lines when I enter status code in Assertion

I am new in API testing and writing my first java script. Every thing works fine till I tried to do assertion and put status Code 200. Then whole script got red lines.
import io.restassured.RestAssured;
import static io.restassured.RestAssured.given;
public class Bases {
public static void main(String[] args) {
RestAssured.baseURI= "https://maps.googleapis.com";
given().
param("input","%2B61293744000").
param("inputtype" , "phonenumber").
param("fields","place_id").
param("key","AIzaSyAPKUY3YgFBlyyQxjh9q9RQ5cDFdDjqlz4").
when().
get("/maps/api/place/findplacefromtext/json").
then().assertThat().statusCode(200);
I just downloaded jars ver 3.2 and it works now. So I believe we can not use update
ver 4.1.2.jars.

using core.Resource in a Dart transformer leads to Build error

i started with the simple_transformer example on how to write a simple Dart Pub Transformer simple_transformer.
This example defines the content to insert into files by specifying it in code
String copyright = "Copyright (c) 2014, the Example project authors.\n";
instead i wanted to use the new (Dart 1.12) Resource class from the core package in order to load this copyright message from a local file (lib/copyright.txt):
static Future<String> loadCopyright() {
var copyrightRessource = new Resource("package:simple_resource_loading_transformer/copyright.txt");
return copyrightRessource.readAsString();
}
While invoking this method from the main function works
main() {
print('load copyright.txt');
//this loads the resource as expected
InsertCopyright.loadCopyright().then(
(String code)=>print(code)
);
}
, invoking it in the Transformer's apply-method fails when trying to transform another package (which is what Transformers are for). You'll get a
Build error: Transform InsertCopyright on {your project} threw error: Load Error for "package:simple_resource_loading_transformer/copyright.txt": SocketException: OS Error: Connection refused
How do i make Resource work in a Pub Transformer? Or is this missing functionality that still should be added to Dart?
Update
so here is the working solution based on the proposed usage of the Transform API
static Future<String> loadCopyright(Transform transform) {
var copyrightAssetId = new AssetId('simple_resource_loading_transformer', 'lib/copyright.txt');
return transform.readInputAsString(copyrightAssetId);
}
The Transform instance comes from the parameter of your Transformer.apply method.
See https://github.com/dart-lang/barback/issues/65#issuecomment-142455056
You should really be using the Barback Transform APIs to load assets anyway. That's what it's for.

How do I pass arguments to Web UI's dwc compiler from build.dart?

I want to turn off URL rewriting by dwc (the Web UI compiler). I run dwc via build.dart. How do I pass an argument from build.dart down to dwc?
Any arguments after -- will be passed to dwc.
void main() {
var args = new List.from(new Options().arguments);
args.addAll(['--', '--no-rewrite-urls']);
build(args, ['web/clock_page.html']);
}
Read more about dwc.

Resources