Dart:
Printing debug output

How to:

In Dart, you can print debug output using the print() function. Here’s how to output simple messages and variable values:

void main() {
  String greeting = "Hello, Dart!";
  print(greeting); // Prints: Hello, Dart!

  int number = 42;
  print('The number is $number.'); // Prints: The number is 42.
}

For structured data, like lists or objects, Dart’s toString() method may not provide enough detail. In those cases, you can use the jsonEncode function from Dart’s dart:convert library to convert the data to a JSON string for more readable output:

import 'dart:convert';

void main() {
  var user = {
    'name': 'John Doe',
    'age': 30,
    'emails': ['[email protected]', '[email protected]'],
  };

  print(jsonEncode(user));
  // Prints: {"name":"John Doe","age":30,"emails":["[email protected]","[email protected]"]}
}

When more sophisticated debugging capabilities are needed, such as logging with different levels of importance (info, warning, error), you can use third-party libraries like logger. Here’s how to use it:

  1. Add logger to your pubspec.yaml:
dependencies:
  logger: ^1.0.0
  1. Use logger in your Dart code:
import 'package:logger/logger.dart';

var logger = Logger();

void main() {
  logger.d("This is a debug message");
  logger.w("This is a warning message");
  logger.e("This is an error message");
}

The output will be more informative, showing the level of the message and the message itself, making it easier to distinguish between different kinds of log messages.