'How can I print multiple objects to console.log with dart?
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?
Solution 1:[1]
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);
Solution 2:[2]
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]);
Solution 3:[3]
use
print("$x,$y,$z");
or
stdout.write("$x,$y,$z");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Lance Fisher |
Solution 3 | sumiit yadav |