Console.log in Dart Language

ConsoleDart

Console Problem Overview


How can I log into the browser console, like console.log in JavaScript, from the Dart language?

Console Solutions


Solution 1 - Console

Simple:

print('This will be logged to the console in the browser.');

A basic top-level print function is always available in all implementations of Dart (browser, VM, etc.). Because Dart has string interpolation, it's easy to use that to print useful stuff too:

var a = 123;
var b = Point(2, 3);
print('a is $a, b is ${b.x}, ${b.y}');

Solution 2 - Console

Also, dart:html allows use of window.console object.

import 'dart:html';

void main() {
  window.console.debug("debug message");
  window.console.info("info message");
  window.console.error("error message");
}

Solution 3 - Console

It’s easy! Just import the logging package:

import 'package:logging/logging.dart';

Create a logger object:

final _logger = Logger('YourClassName');

Then in your code when you need to log something:

_logger.info('Request received!');

If you catch an exception you can log it and the stacktrace as well.

_logger.severe('Oops, an error occurred', err, stacktrace);

Logging package documentation : https://github.com/dart-lang/logging

Solution 4 - Console

Simple: print("hello word"); or debugPrint(" hello word);

Solution 5 - Console

When you use only Dart without Flutter, then this is a good and easy solution:

void log(var logstr) {
  stdout.writeln("-> " + logstr.toString());
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreas KöberleView Question on Stackoverflow
Solution 1 - ConsolemunificentView Answer on Stackoverflow
Solution 2 - ConsoleChris BuckettView Answer on Stackoverflow
Solution 3 - ConsoleMark MadejView Answer on Stackoverflow
Solution 4 - Consolethanh tùng trầnView Answer on Stackoverflow
Solution 5 - ConsoleLars LehmannView Answer on Stackoverflow