dart:html not found -> cannot use WebSockets - dart

I'm trying to build a WebSocket client app for my C# server and fail already at importing basic dart libraries:
bin/rpc_dart_test.dart:2:8: Error: Not found: 'dart:html'
import 'dart:html';
^
bin/rpc_dart_test.dart:50:16: Error: Couldn't find constructor 'WebSocket'.
var ws = new WebSocket("ws://localhost:7077/ws");
^^^^^^^^^
bin/rpc_dart_test.dart:51:24: Error: 'MessageEvent' isn't a type.
ws.onMessage.listen((MessageEvent e) {
^^^^^^^^^^^^
bin/rpc_dart_test.dart:54:38: Error: Undefined name 'WebSocket'.
if (ws != null && ws.readyState == WebSocket.OPEN) {
^^^^^^^^^
How can I make this work? Just removing the import line as recommended in some Google results only makes the first error go away, but doesn't help in general. Changing from dart:html to package:http/http.dart neither works, because there is no WebSocket class in that package.

Related

How to uses packages for the flutter app in the dart server?

My project has 3 sub-project including
dart_server
flutter_app
share
The share project is about objects, config, and paths, ... which was used in the dart_server and flutter_app projects.
In the share project. Some objects contain a package used for the flutter like the [get][1] package and this object is used for both projects (dart_server and flutter_app). When using this object in the dart_server project, it will error and the terminal will show an error too long.
error example:
/C:/src/flutter/packages/flutter/lib/src/gestures/velocity_tracker.dart:299:35: Error: Type 'Offset' not found.
void addPosition(Duration time, Offset position) {
^^^^^^
/C:/src/flutter/packages/flutter/lib/src/gestures/velocity_tracker.dart:317:3: Error: Type 'Offset' not found.
Offset _previousVelocityAt(int index) {
^^^^^^
/C:/src/flutter/packages/flutter/lib/src/gestures/velocity_tracker.dart:24:58: Error: Undefined name 'Offset'.
static const Velocity zero = Velocity(pixelsPerSecond: Offset.zero);
^^^^^^
/C:/src/flutter/packages/flutter/lib/src/gestures/velocity_tracker.dart:27:9: Error: 'Offset' isn't a type.
final Offset pixelsPerSecond;
^^^^^^
This can reproduce by creating a dart server project and import any package for the flutter like [get][1] in the file that will run and run this server by dart run.
How to use this object that contains packages for the flutter app?

How to import jregex library to ballerina app

I’m trying to validate regex in Ballerina using library samjs/jregex from https://central.ballerina.io/samjs/jregex
I’m trying to get this executed:
import ballerina/io;
import samjs/jregex;
public function main() {
//io:println(Commons:isValidDateTime("dupa"));
jregex:Pattern p = jregex:compile(regex = ".r");
jregex:Matcher m = p.matcher(input = "br");
var b = m.matches();
io:println(b);
}
I have run command ballerina pull samjs/jregex
My Ballerina.toml is:
[project]
org-name= "organization"
version= "0.1.0"
[platform]
target= "java8"
[[platform.libraries]]
artifactId = "mssql-jdbc"
version = "8.2.0"
path = "./mssql-jdbc-8.2.0.jre8.jar"
[dependencies]
However this is not working, because I’m getting error:
error: module not found: ballerinax/java:*_java8 or ballerinax/java:*_anyerror: samjs/jregex:0.2.0::/src/jregex/matcher.bal:1:1: cannot resolve module 'ballerinax/java'
error: module not found: ballerinax/java:*_java8 or ballerinax/java:*_any
error: samjs/jregex:0.2.0::/src/jregex/pattern.bal:1:1: cannot resolve module 'ballerinax/java'
error: samjs/jregex:0.2.0::/src/jregex/matcher.bal:85:29: undefined module 'java'
error: samjs/jregex:0.2.0::/src/jregex/matcher.bal:85:29: undefined function 'fromString'
error: samjs/jregex:0.2.0::/src/jregex/matcher.bal:128:69: undefined module 'java'
error: samjs/jregex:0.2.0::/src/jregex/matcher.bal:128:69: undefined annotation 'Method'
it seems that import of jregex requires ballerinax, however I cannot import to code anything that is not directly used (Ballerina does not allow for "empty" imports).
Does anybody knows how to import this regex library to make it working in application?

How to solve Not found: 'dart:ui' error while running integration tests on Flutter

I have an app, it is very simple and have just one widget. It is working fine, however when I run integration test by calling:
$ flutter drive --target=test_driver/app.dart
I get the following error:
file:///Users/myuser/flutter/packages/flutter_test/lib/src/accessibility.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/binding.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/matchers.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/matchers.dart:9:8: Error: Not found: 'dart:ui'
import 'dart:ui';
^
file:///Users/myuser/flutter/packages/flutter_test/lib/src/test_pointer.dart:12:1: Error: Not found: 'dart:ui'
export 'dart:ui' show Offset;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/binding.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show window;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/box.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show lerpDouble;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/debug_overflow_indicator.dart:6:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/editable.dart:8:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show TextBox;
^
file:///Users/myuser/flutter/packages/flutter/lib/src/rendering/error.dart:5:8: Error: Not found: 'dart:ui'
import 'dart:ui' as ui show Paragraph, ParagraphBuilder, ParagraphConstraints, ParagraphStyle, TextStyle;
^
Stopping application instance.
Driver tests failed: 254
Note that when I run the app from Android Studio, it runs successfully. But when I run from terminal using the command quoted above, the app shows a white screen and it doesn't move from there until I get the error on my terminal.
Assuming it's a path issue, like test_driver not finding flutter packages like dart:ui, how can I make sure test_driver knows where dart:ui is ?
Make sure that the import is set to this:
import 'package:test/test.dart';
instead of this:
import 'package:flutter_test/flutter_test.dart';
The integration tests can have no imports to your main App code or other flutter code that runs in the App - otherwise they will fail with your seen error.
Have a read of https://flutter.io/cookbook/testing/integration-test-introduction/ as this gives an example of integration tests with an app starting point that allows you to run setup code before your actual driver tests run if this is what you are looking to do. Otherwise don't use key values that use constants from your main code (as mentioned here http://cogitas.net/write-integration-test-flutter/).
This is old post, but I found another possible reason why test gave me this error:
When I was creating new Bloc class I created constructor with #required annotation and when I used Option+Enter for importing appropriate library, Android Studio imported 'package:flutter/cupertino.dart' library instead of 'package:meta/meta.dart' and that is the reason my unit test didn't start in the first place.
After importing correct library, all tests have passed!
Happy testing your code! :D
Here is the solution for adding these test cases:
In Android studio in the run dropdown you
select Edit Configurations
Press the + button and select Flutter test
Make sure the Test scope is All in file and point it at your test file.
You can now run the individual test file and also debug it in android studio by selecting this configuration in the run drop-down.
Remove all imports to package:flutter/... from the test driver's code, like:
import 'package:flutter/widgets.dart';
I was getting these erros because I was trying to import a widget on a test_driver file. I also got the same error if I try to use find.byWidget.
I was trying to import a widget or use find.byWidget because I wanted to check the existence of a widget on a screen.
These erros are pretty much similar to
The built-in library 'dart:ui' is not available on the stand-alone VM.
Then, in order to check the existence of a widget on screen in a test_driver file, I had to use find.byKeyValue. For example, given the following dummy widget, defined in a file within my app:
class MyDummyWidget extends StatelessWidget {
MyDummyWidget(): super(key: new Key('MyDummyWidget'));
#override
Widget build(BuildContext context) {
return Center();
}
}
To check if it is being displayed on screen, I define the following test within the test_driver:
void main() {
group('My tests', () {
FlutterDriver driver;
SerializableFinder myDummyWidget = find.byValueKey('MyDummyWidget');
setUpAll(() async {
driver = await FlutterDriver.connect();
});
tearDownAll(() async {
if (driver != null) {
driver.close();
}
});
test('check if MyDummyWidget is being displayed', () async {
await driver.waitFor(myDummyWidget);
});
});
}
Where the key definition is the required one in the first file, and then, the find.byValueKey and await driver ones are the essentials in the test file.
Don't simply run the test. It is running the pp virtually. So we can't import dart: UI. It'll give the error. Use command
flutter drive --target=test_driver/app.dart to test it.
In Android Studio, right-click on the test file in the Project tree and select "Run". You should see 2 options:
Make sure you select the second option: "tests in widget_test..."
If you select the first option you will see the Error: Not found: 'dart:ui' and you lose the option to make a selection from the Run context menu.
Finally, I got run widget tests in my app.
This soluction worked for me.
I found out why, I chose the first option instead of the flutter test. how silly I was, now removing this wrong test type from configuration and run with flutter test works now.
See the image about run in Android Studio
Just check that you don't have imports which load flutter packages.
In my case I had 'cupertino' package import from my file app_keys.dart which listed all keys in a format Key('<something>').
BUT!! UI tests use Key('<something>') constants for Finder but Integration tests - simple strings.
Reference

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

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

Is dart:intl broken? / DateFormat to complicated

I using Dart Editor 0.3.7_r18717 (means the editor is up to date (Feb 2013)
import 'dart:intl';
Shows an error "Cannot find referenced source"
If I import the package via pub
import 'package:intl/intl.dart';
then compiling to JS dart2js fails with some weird errors...
What I want is simply something like this:
final String time = new DateFormat("HH:mm:ss","de").format(new DateTime.now());
It's not clear at all why the locale is needed in such case...
thx
[Update]
OK - I know "weird errors" means nothing - so here is the output if I import the following two packages:
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
String getTime() {
final String time = new DateFormat("HH:mm:ss","en_US").format(new DateTime.now());
//final String time = "20:05:00";
return time;
}
Output:
Running dart2js...
Using snapshot /Developer/dart/dart-sdk/lib/_internal/compiler/implementation/dart2js.dart.snapshot
Wrote /Users/mikemitterer/dart/WebSockets.SAMPLE/web/out/jrelaisui.html_bootstrap.dart.js [410.0kb written in 5.3 seconds]
build.dart --machine --changed=web/jrelaisui.dart
file:/Users/mikemitterer/dart/WebSockets.SAMPLE/build.dart
build.dart returned error code 255
Uncaught Error: NoSuchMethodError : method not found: '_addFromInteger#0x36924d72'
Receiver: null
Arguments: [80]
Stack Trace:
#0 Object.noSuchMethod (dart:core-patch:1737:25)
#1 int.+ (dart:core-patch:1324:33)
#2 Parser.translateCharacter (package:analyzer_experimental/src/generated/parser.dart:4380:167)
#3 Parser.computeStringValue (package:analyzer_experimental/src/generated/parser.dart:376:33)
#4 Parser.parseStringLiteral (package:analyzer_experimental/src/generated/parser.dart:3451:76)
#5 Parser.parsePrimaryExpression (package:analyzer_experimental/src/generated/parser.dart:3163:32)
#6 Parser.parseAssignableExpression (package:analyzer_experimental/src/generated/parser.dart:837:51)
#7 Parser.parsePostfixExpression (package:analyzer_experimental/src/generated/parser.dart:3061:51)
...
Output - Clean Up Source:
build.dart --machine --clean
file:/Users/mikemitterer/dart/HelloWebComponent/build.dart
build.dart returned error code 255
'package:html5lib/src/encoding_parser.dart': Error: line 65 pos 14: class 'EncodingBytes' overrides function 'skip' of super class 'Iterable' with incompatible parameters
String skip([CharPreciate skipChars]) {
^
build.dart --machine --clean
file:/Users/mikemitterer/dart/todomvc/build.dart
build.dart returned error code 255
'package:html5lib/src/encoding_parser.dart': Error: line 65 pos 14: class 'EncodingBytes' overrides function 'skip' of super class 'Iterable' with incompatible parameters
String skip([CharPreciate skipChars]) {
...
Thats what I mean with "weird" :-)
The package syntax is the correct one, this should work:
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
main() {
print(new DateFormat.yMd().format(new DateTime.now()));
}
What "weird errors" are you talking about?
import "dart:intl"; won't work to import it because it's not one of the "core" dart libraries, it has to be imported as a package. The errors you're listing in dart2js don't look like they're coming from internationalization code, but from web_ui and from the analyzer_experimental package. What happens if you comment out the lines that use internationalization and compile using dart2js?

Resources