How to use animationFrame in Dart? - dart

The following code throws the exception "type '([int]) => void' is not a subtype of type 'RequestAnimationFrameCallback' of 'callback'."
import 'dart:html';
void main() {
window.animationFrame.then((time) => print("test"));
}
If I change window.animationFrame.then to window.requestAnimationFrame, everything works as expected. Am I misunderstanding how Dart futures work?

Usage looks right.
Either you are running an older version that doesn't yet implement the behavior that is specified in the documentation or it is simply a bug. I would go ahead and file an issue on http://dartbug.com/new

Related

Is there a way to call extension methods from debug console?

Debugging with extension methods seems to be a bit problematic currently.
Consider the following code:
import 'dart:developer';
void main() {
final a = A();
workaround = a.extMethod;
debugger();
a.normalMethod();
a.extMethod();
}
class A {
String normalMethod() => 'hello from class';
}
extension AX on A {
String extMethod() => 'hello from extension';
}
When debugging this, stepping into both methods (normalMethod() and extMethod()) will work fine, the only difference is that this isn't available for extMethod() for some reason (there is #this instead, but it can't be used anyhow).
The extension method (extMethod()) is impossible to use for evaluations in the debug console, though.
a.normalMethod()
"hello from class"
a.extMethod()
Unhandled exception:
NoSuchMethodError: Class 'A' has no instance method 'extMethod'.
Receiver: Instance of 'A'
Tried calling: extMethod()
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:38:5)
#1 Eval ()
workaround()
"hello from extension"
I have found kind of a workaround as you can see, but it has to be in place before running the program so I'd definitely prefer a better method that can be used on fly without any preparation.
I have found two very old issues describing this exact problem but both are closed and from what I understand, unresolved.
https://github.com/dart-lang/sdk/issues/44801
https://github.com/dart-lang/sdk/issues/44472
I'm using Dart 2.18.6 (latest stable).

How to test whether tree-shaking is applied to unused funciton in dart

Consider following dart code:
main.dart
import 'module.dart';
main() {
foo();
}
module.dart
foo() {
print('foo');
}
bar() {
print('bar');
}
I expect bar to be eliminated from the final build by dart compiler with tree-shaking since it's statically analyzable that the function was not used in main.
I tried the method answered in related S/O question.
Using devtools by running dart run --observe main.dart, I still see bar was included.
How can I know whether bar was eliminated from the final build or not?
Alternatively, I want someone with an understanding of dart compiler to convince me whether the function was eliminated or not, so I don't need to worry about checking it myself.

Undefined class 'PriorityQueue'

import 'dart:collection';
void main(){
PriorityQueue<double> p;
}
This code wont check, as Dart analyser cannot find PriorityQueue in collection. I believe that PriorityQueue is in there, so is there something wrong with my analyser? It does sometimes produce false errors, do I need to reset the cache? (I've tried, but maybe I didn't do it right)
PriorityQueue is not in dart:collection, it is in a different "collection" unit, the package:collection package, which you'll need to import as a dependency.
Like this
in pubspec.yaml:
dependencies:
collection: ^1.14.13
Then import 'package:collection/collection.dart'; in your code.

In dart web projects, shouldn't type and reference warnings be errors?

In dart, when developing a web application, if I invoke a method with a wrong number of arguments, the editor shows a warning message, the javascript compilation however runs successfully, and an error is only raised runtime. This is also the case for example if I refer and unexistent variable, or I pass a method argument of the wrong type.
I ask, if the editor already know that things won't work, why is the compilation successful? Why do we have types if they are not checked at compile time? I guess this behaviour has a reason, but I couldn't find it explained anywhere.
In Dart, many programming errors are warnings.
This is for two reasons.
The primary reason is that it allows you to run your program while you are developing it. If some of your code isn't complete yet, or it's only half refactored and still uses the old variable names, you can still test the other half. If you weren't allowed to run the program before it was perfect, that would not be possible.
The other reason is that warnings represent only static type checking, which doesn't know everything about your program, It might be that your program will work, it's just impossible for the analyser to determine.
Example:
class C {
int foo(int x) => x;
}
class D implements C {
num foo(num x, [num defaultValue]) => x == null ? defaultValue : x;
}
void bar(C c) => print(c.foo(4.1, 42)); // Static warning: wrong argument count, bad type.
main() { bar(new D()); } // Program runs fine.
If your program works, it shouldn't be stopped by a pedantic analyser that only knows half the truth. You should still look at the warnings, and consider whether there is something to worry about, but it is perfectly fine to decide that you actually know better than the compiler.
There is no compilation stage. What you see is warning based on type. For example:
This code will have warning:
void main() {
var foo = "";
foo.baz();
}
but this one won't:
void main() {
var foo;
foo.baz();
}
because code analyzer cant deduct the type of foo

No top-level method 'spawnFunction' declared

I'm trying to use Isolates in Dart. The tutorials from dartlang.org seem to use the function spawnFunction. But that does not seem to work for me. And I cant find any docs about this.
import 'dart:isolate';
void doThing() {
print('Hello!');
}
main() {
spawnFunction(doThing);
}
.
Unhandled exception:
No top-level method 'spawnFunction' declared.
The docs from api.dartlang.org mention Isolate.spawn but I get an error saying there is no static method spawn declared.
Did I miss something? A link to appropriate docs (if any) would be appreciated.
Thanks!
Isolate.spawn is indeed the new way of creating isolates. Your example would need to be rewritten as:
import 'dart:isolate';
void doThing(_) {
print("Hello!");
}
main() {
Isolate.spawn(doThing, null);
}
See https://groups.google.com/a/dartlang.org/forum/#!topic/misc/EVUMkZXFXtY for the breaking change announcement.

Resources