To log data to the Flutter console, you can use the print() function or the debugPrint() method for more control.

Using print()

The print() function outputs messages to the console.

For example:

print("Hello, Flutter!");

Using debugPrint()

debugPrint() is useful for logging large data without being truncated.

For example:

debugPrint("This is a debug message.");

The log() method from the dart:developer library is used to log messages to the console. This is especially useful when you need structured logging with additional metadata like a name or stack trace.

Using log()

import 'dart:developer';

void main() {
  String message = "This is a log message";
  log(message, name: 'MyApp');
}

Features of log()

  • message: The main log content.
  • name: A tag to identify the source of the log.
  • error and stackTrace: Additional details for errors.

Example with Error Logging:

try {
  throw Exception("Sample error");
} catch (e, stackTrace) {
  log("An error occurred", name: "ErrorLogger", error: e, stackTrace: stackTrace);
}

Why Use log()?

Unlike print(), the log() method integrates well with developer tools, providing more context and better debugging information.

Support On Demand!

Flutter

Related Q&A