Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print to the console with Dart?

Tags:

dart

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) ?

like image 557
Seth Ladd Avatar asked Jan 09 '14 18:01

Seth Ladd


People also ask

What is print to console?

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.

How do you print in flutter?

you can simply use print('whatever you want to print') same as console. log() in javascript.


5 Answers

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.

like image 131
Seth Ladd Avatar answered Oct 05 '22 11:10

Seth Ladd


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.

like image 36
Fox32 Avatar answered Oct 05 '22 11:10

Fox32


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 and stderr. Generally, this is done using print() statements, or by importing dart:io and invoking methods on stderr and stdout. 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’s foundation library. This is a wrapper around print that throttles the output to a level that avoids being dropped by Android’s kernel.

The 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).

like image 28
Blasanka Avatar answered Oct 05 '22 12:10

Blasanka


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.

like image 22
Prajwal Avatar answered Oct 05 '22 10:10

Prajwal


Dart print() function works differently in different environment.

  • print() when used in console based application it outputs in the terminal console
  • print() when used in web based application it outputs to the developer console.
void main() {
  print("HTML WebApp");
}

like image 20
SoftRock Avatar answered Oct 05 '22 12:10

SoftRock