console.error() in Dart? - dart

How do I do this console.error(foo); in Dart?
The print() method gets quite close, but it's not exactly what I want. I want to have the notification of an error and the icon along with the stack trace.

It's actually very simple to do in Dart:
import 'dart:html';
main() {
window.console.error('Something bad occurred');
}

Related

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, how do I add a ">>" before the input created by stdin.readLineSync()?

I'm writing a Dart console app for the first time and I've searched the Internet everywhere and can't find the proper answer.
In Python, the function raw_input(); takes user input from stdin in a similar way to Dart's stdin.readLineSync();, however raw_input(); takes a parameter: If I do something like raw_input(">>");, the CLI does something like:
>> _
In Dart, however, I cannot find a way to do something similar with stdin.readLineSync();, it simply outputs:
_
Is there a simple way for a new Dart user to accomplish this? Thank you for your time.
This might do what you want:
import 'dart:io';
main() {
stdout.write('>> ');
var result = stdin.readLineSync();
print(result);
}
write does not automatically add a newline.
A simpler way is to use the DCli package and its ask function:
var response = ask('>>');
print(response);
The ask function provides a number of other options that help capturing data from the cli such as the functions 'confirm' and 'menu'.
https://onepub.dev/packages/dcli

Dart language trignometric functions

I am creating a scientific calc app in DART . i dont knw how to use trignometric functions like sine , cosine . i used "math.sin()" , but it throws an exception "NO top-level getter math.get declared " how to solve it ? thanks in advance
To use trigonometric functions in Dart, import the dart:math library. For example:
import 'dart:math';
main() {
print(sin(pi));
}
If you want, you can import with a prefix to avoid namespace collisions:
import 'dart:math' as Math;
main() {
print(Math.sin(Math.pi));
}

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