8

In javascript, if I have 3 variables like this:

var x = 1
var y = 'cat'
var z = {color: 'blue'}

I can log all of them like this:

console.log('The values are:', x, y, z)

In dart, you can import 'dart:html' and it will map print to console.log in the compiled javascript. But print only takes one parameter - not 3. This dart will fail when the compiled js runs in the browser:

print('The values are:', x, y, z)

The only thing I can think to do is stringify these arguments and join them into one string, and print that. But then I lose Chrome's ability to expand objects that are printed to the console.

Is it possible to print multiple objects with one print statement (or similar statement)? If so, how?

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
cilphex
  • 6,006
  • 6
  • 34
  • 44

3 Answers3

10

What about: ?

print('The values are: ${[x, y, z]}')

or

print('The values are: $x, $y, $z')

or

['The values are:', x, y, z].forEach(print);
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Both versions print all objects as part of the string, instead of as independent objects. An actual javascript object (e.g. `{one: 1, ...}`) would not be expandable printed this way. – cilphex Nov 05 '14 at 11:21
  • 1
    For each element `toString()` would be called and the result added to the string. I'm not a JavaScript programmer and not aware about how this might be handled differently there. I added yet another variant maybe this is more what you expect. – Günter Zöchbauer Nov 05 '14 at 11:24
  • 1
    The third version prints all values, but prints objects as strings instead of objects. Maybe it is not possible. – cilphex Nov 05 '14 at 11:39
  • 2
    It is what print does. It calls `toString()` on the passed values. Mybe you just want to use `window.console.debug` instead of `print` but produces only a different output in Dartiums console, not in the console view of DartEditor. – Günter Zöchbauer Nov 05 '14 at 12:03
  • 1
    great answer!) @GünterZöchbauer, thanks again!) especially for the last one) – Timur Fayzrakhmanov Mar 25 '15 at 08:19
0

In Dart, you can import dart:html which gives you access to the window.console. Unfortunately, its log method can only take one argument. You can wrap your objects in a list though:

import 'dart:html';

var x = 1
var y = 'cat'
var z = {color: 'blue'}

window.console.log([x, y, z]);
Lance Fisher
  • 25,684
  • 22
  • 96
  • 122
0

use

print("$x,$y,$z");

or

stdout.write("$x,$y,$z");