Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to print a console message with Flutter?

Tags:

flutter

dart

I'm debugging an app, but I need to know some values in the fly, I was wondering if there's a way to print a message in console like console.log using Javascript.

I appreciate the help.

like image 416
Joseph Arriaza Avatar asked Aug 09 '18 13:08

Joseph Arriaza


People also ask

How do I print from console?

You should use the console. log() method to print to console JavaScript. The JavaScript console log function is mainly used for code debugging as it makes the JavaScript print the output to the console.

How do you print a message in Dart?

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

What is debug print in flutter?

DebugPrintCallback debugPrint. read / write. Prints a message to the console, which you can access using the "flutter" tool's "logs" command ("flutter logs"). See also: DebugPrintCallback, for function parameters and usage details.


4 Answers

print() is probably what you are looking for. Here's some more info on debugging in flutter.

like image 53
Ringil Avatar answered Oct 17 '22 07:10

Ringil


You can use

print() 

function or

debugPrint()

The debugPrint() function can print large outputs.

like image 43
Jaswant Singh Avatar answered Oct 17 '22 07:10

Jaswant Singh


There are more helpful methods in import 'dart:developer' library and one of them is log().

example:

int i = 5;
log("Index number is: $i");

//output
[log] Index number is: 5

void log(String message, {DateTime time, int sequenceNumber, int level = 0, String name = '', Zone zone, Object error, StackTrace stackTrace})

Emit a log event.

This function was designed to map closely to the logging information collected by package:logging.

[message] is the log message
[time] (optional) is the timestamp
[sequenceNumber] (optional) is a monotonically increasing sequence number
[level] (optional) is the severity level (a value between 0 and 2000); see the package:logging Level class for an overview of the

possible values [name] (optional) is the name of the source of the log message [zone] (optional) the zone where the log was emitted [error] (optional) an error object associated with this log event [stackTrace] (optional) a stack trace associated with this log event

Read more.:

print() is from dart:core and its implementation:

/// Prints a string representation of the object to the console.
void print(Object object) {
  String line = "$object";
  if (printToZone == null) {
    printToConsole(line);
  } else {
    printToZone(line);
  }
}

debugPrint():

/// Prints a message to the console, which you can access using the "flutter"
/// tool's "logs" command ("flutter logs").
///
/// If a wrapWidth is provided, each line of the message is word-wrapped to that
/// width. (Lines may be separated by newline characters, as in '\n'.)
///
/// 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

// read more here: https://api.flutter.dev/flutter/foundation/debugPrint.html
DebugPrintCallback debugPrint = debugPrintThrottled;


/// Alternative implementation of [debugPrint] that does not throttle.
/// Used by tests. 
debugPrintSynchronously(String message, { int wrapWidth })

/// Implementation of [debugPrint] that throttles messages. This avoids dropping
/// messages on platforms that rate-limit their logging (for example, Android).
void debugPrintThrottled(String message, { int wrapWidth })

Read more.

Note that only the print() is taking any type and print to the console. debugPrint() and log() only take String. So, you have to add .toString() or use string interpolation like I shown in provided example snippet.

like image 18
Blasanka Avatar answered Oct 17 '22 06:10

Blasanka


I tend to do something similar to this

Foo foo;
try{
    foo = _someMethod(); //some method that returns a new object
} catch (e) {
    print('_someMethod: Foo Error ${foo.id} Error:{e.toString()}'); /*my custom error print message. You don't need brackets if you are printing a string variable.*/
}
like image 9
F-1 Avatar answered Oct 17 '22 06:10

F-1