I'd like my Dart program to print to the dev console of my browser. How can I print to the console (DevTools's console, for example) ?
Print to Console in Python. To print strings to console or echo some data to console output, use Python inbuilt print() function. print() function can take different type of values as argument(s), like string, integer, float, etc., or object of a class type.
you can simply use print('whatever you want to print') same as console. log() in javascript.
Use print()
to print a string to the console of your browser:
import 'dart:html';
main() {
var value = querySelector('input').value;
print('The value of the input is: $value');
}
You will see a message printed to the developer console.
If you simlpy want to print text to the console you can use print('Text')
.
But if you want to access the advanced fatures of the DevTools console you need to use the Console
class from dart:html
: Console.log('Text')
.
It supports printing on different levels (info, warn, error, debug). It also allows to print tables and other more advanced features. Note that these are not supported in every browser! It's sad that the documentation about the Console
class is incomplete, but you can take a look at the documentation of Chrome here and here.
There is log()
from import 'dart:developer'
library also.
example:
int name = "Something";
log("ClassName: successfully initialized: $name");
//output
[log] ClassName: successfully initialized: Something
Please note that log
and debugPrint
taking a value of String not like print
. So, you have to add .toString()
at the end or use with String interpolation like I used in above example.
From doc:
You have two options for logging for your application. The first is to use
stdout
andstderr
. Generally, this is done usingprint()
statements, or by importingdart:io
and invoking methods onstderr
andstdout
. For example:
stderr.writeln('print me');
If you output too much at once, then Android sometimes discards some log lines. To avoid this, use
debugPrint()
, from Flutter’sfoundation
library. This is a wrapper aroundThe other option for application logging is to use the
dart:developer
log()
function. This allows you to include a bit more granularity and information in the logging output. Here’s an example:import 'dart:developer' as developer; void main() { developer.log('log me', name: 'my.app.category'); developer.log('log me 1', name: 'my.other.category'); developer.log('log me 2', name: 'my.other.category'); }
You can also pass application data to the log call. The convention for this is to use the error: named parameter on the
log()
call, JSON encode the object you want to send, and pass the encoded string to the error parameter.import 'dart:convert'; import 'dart:developer' as developer; void main() { var myCustomObject = ...; developer.log( 'log me', name: 'my.app.category', error: jsonEncode(myCustomObject), ); }
If viewing the logging output in DevTool’s logging view, the JSON encoded error param is interpreted as a data object and rendered in the details view for that log entry.
read more(It's cool like a tutorial).
If you are here for Flutter
, there's debugPrint
which you should use.
Here's the doc text for the same.
/// Prints a message to the console, which you can access using the "flutter"
/// tool's "logs" command ("flutter logs").
/// By default, this function very crudely attempts to throttle the rate at
/// which messages are sent to avoid data loss on Android. This means that
/// interleaving calls to this function (directly or indirectly via, e.g.,
/// [debugDumpRenderTree] or [debugDumpApp]) and to the Dart [print] method can
/// result in out-of-order messages in the logs.
You might get SDK version constraint as it is only for 2.2 and above.
Dart print()
function works differently in different environment.
print()
when used in console based application it outputs in the terminal consoleprint()
when used in web based application it outputs to the developer console.void main() {
print("HTML WebApp");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With