Dart Js-interop: how to declare function with callback? - dart

I'm using node.js API from compiled Dart app. With package:js bindings for this api's:
#JS('http')
library http;
import "package:js/js.dart";
external Server createServer(
[ void requestListener(IncomingMessage request, ServerResponse response)])
And calling this from main.dart:
import "package:js/js.dart";
import "node_partly.dart" as http;
main() async {
http.Server server = http.createServer(allowInterop(handler));
server.listen(9876);
}
void handler(http.IncomingMessage request, http.ServerResponse response){
response.write("Hello, from dart server! \n");
response.write("You asked for ${request.url}");
print(request.url);
response.end();
}
Everything works fine.
But. Analyzer fails with error
WARNING: The argument type 'JSFunction' cannot be assigned to the
parameter type '(IncomingMessage, ServerResponse) → void'. (
example/main.dart:5)
The problem is that allowInterop produces "JSFunction" - deprecated class for internal use.
And from point of view of annotations - there should be normal "Function" class.
This is annoying if to pay attention to this. And if I will not pay attention to analyzer errors - I may leave real error in my code.
How can I fix it?
P.S. Using latest dart 1.16
Update: created issue at github https://github.com/dart-lang/sdk/issues/26371

Related

Using java in rascal

I have made a java function genImage(List<String lines) which is located in class vis. I am trying to import it into my rascal code, but it won't work. This is the last of my efforts to import it:
#javaClass{visualization.vis}
java void genImage(list[str] lines);
The error I get:
Cannot link method visualization.vis because: visualization.vis.(io.usethesource.vallang.IValueFactory)
Advice: |http://tutor.rascal-mpl.org/Errors/Static/JavaMethodLink/JavaMethodLink.html%7C
The #javaClass tag must point to a fully qualified classname, including the package and the class. It seems it's the class you are missing, right?

JS Interop works with DDC but not with Dart2JS

Dart 2.7
build_runner: 1.7.3
build_web_compilers: 2.9.0
I've created an interface for GoJS using the package js: https://github.com/jodinathan/gojs_dart/
The problem is that it works flawlessly in DDC but throws an exception when using with Dart2JS.
Here is a small repro you can check:
https://github.com/jodinathan/gojs_dart/tree/master/example/flowchart
The error seems to be in the line ..bind(GoJSBinding('text').makeTwoWay())
I think it can't find the bind() function, however, it can when in DDC.
Any ideas on how I could solve this?
The issue is in the dart-sdk: https://github.com/dart-lang/sdk/issues/40434
It looks like dart2js requires an external factory constructor on all classes while dartdevc does not. So for instance for the first error it hits today you have a class like this:
#JS('Node')
class GoJSNode extends GoJSPart {}
And it should look like this:
#JS('Node')
class GoJSNode extends GoJSPart {
external factory GoJSNode();
}

dart.io examples from dartlang threw 'uncaught' error on dartpad (beginner level)

Im trying to follow tutorials from "Dart for Absolute Beginner" book and from dartlang https://www.dartlang.org/tutorials/dart-vm/cmdline "stdin" examples to accept keyboard input however dartpad will show 'uncaught' for every examples i tried.
Sample code:
import 'dart.io'
void main() {
stdout.writeln('Type something');
String input = stdin.readLineSync();
stdout.writeln('You typed: $input');
}
Can somebody point me to what I should add ? A try and catch block ? How do I do that or get to that ? I am only on page 41 for the "Dart for Absolutely Beginner" book so don;t expect me to know much.
DartPad transpiles Dart to JavaScript before it can execute the code.
dart:io is limited to console applications.
dart:html provides abstraction of the API available in the browser, but there is no equivalent to stdin or readLine in the browser.
you can use that website it's support dart:io
https://replit.com/languages/dart

Dart : Isolate not working when using html import

I found this very strange and unfortunate behavior in Dart. When I import 'dart:html' in my main file, my Isolate stops working.
With my file "isolate.dart" :
main(){
print('BAM');
}
This prints "BAM":
import 'dart:core';
import 'dart:isolate';
void main() {
Isolate.spawnUri(Uri.parse('isolate.dart'), [], null);
}
but this prints nothing :
import 'dart:core';
import 'dart:isolate';
import 'dart:html';
void main() {
Isolate.spawnUri(Uri.parse('isolate.dart'), [], null);
}
How can I get Isolate to work while using the html import?
UPDATE :
I've found this code https://github.com/TomCaserta/ExampleIsolate and tried to work it to find the problem. It seems like the print() call from the Isolate is causing problems.
This are known bugs/limitations. It is being worked on.
Currently it is not possible to access functionality of the 'dart:html' package in an isolate and 'print()' crashes the isolate probably because there is no package with a 'print' functionality available where the command can be redirected to.
The Dart issue tracker seems not to be available currently.
I try again later to add some references.
Some open issues that I think are related:
Print statement inside isolate crashes isolate.
Isolate.spawn not allowed in Dartium DOM isolate
Support spawn of dom isolates in Dartium
Unable to break on breakpoint in library loaded by spawnUri
isolate in dartium not creating httprequest, works when compiled to javascript

Dart Editor: "unused import" errors

For a project, I'm importing a library but I don't use any of the classes in it directly. The goal is to fetch a ClassMirror at runtime to create an instance from it. So I have something like:
import 'controllers.dart';
main() {
ClassMirror controller = getClassFromString(libraryName: 'deck_app', className: 'HomeController');
InstanceMirror instance = controller.newInstance(new Symbol(''), []);
instance.reflectee.sayHey();
}
This gives me an "unused import" error. Idk if this is to be considered a bug. So I'm asking you: do you think this is to be considered as a bug? If not, is there a way I could suppress unused import errors?
What's weird is I thought Dart would tree-shake the source and remove the unused import's code, but it does not. The library is properly imported and available.
The unused import is just a conclusion from the static analyzer. You can ignore it or add a dummy statement to silence the analyzer. This has no effect when you run the application.

Resources