有没有办法用Flutter打印控制台消息?

Jos*_*aza 11 dart flutter

我正在调试应用程序,但是我需要即时了解一些值,我想知道是否有一种方法可以使用Javascript在console.log这样的控制台中打印消息。

感谢您的帮助。

Jas*_*ngh 17

您可以使用

print() 
Run Code Online (Sandbox Code Playgroud)

功能或

debugPrint()
Run Code Online (Sandbox Code Playgroud)

debugPrint() 函数可以打印大量输出。


Rin*_*gil 15

print()可能正是您想要的。这是有关Flutter调试更多信息。


Bla*_*nka 10

import 'dart:developer'库中有更多有用的方法,其中之一是log().

例子:

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

//output
[log] Index number is: 5
Run Code Online (Sandbox Code Playgroud)

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

发出日志事件。

这个函数被设计成紧密映射到 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
Run Code Online (Sandbox Code Playgroud)

可能的值 [name](可选)是日志消息源的名称 [zone](可选)发出日志的区域 [error](可选)与此日志事件关联的错误对象 [stackTrace](可选) ) 与此日志事件关联的堆栈跟踪

阅读更多。

print() 来自 dart:core 及其实现:

/// 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);
  }
}
Run Code Online (Sandbox Code Playgroud)

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 })
Run Code Online (Sandbox Code Playgroud)

阅读更多

请注意,只有print()接受任何类型并打印到控制台。debugPrint()并且log()只取String. 因此,您必须添加.toString()或使用字符串插值,就像我在提供的示例片段中所示。


F-1*_*F-1 8

我倾向于做类似的事情

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.*/
}
Run Code Online (Sandbox Code Playgroud)


Abe*_*bal 8

使用调试打印以避免登录生产应用程序。

debugPrint("Message");
Run Code Online (Sandbox Code Playgroud)

您还可以禁用或更改 main.dart 或任何其他文件中的调试打印实现,如下所示:

debugPrint = (String message, {int wrapWidth}) 
{
    debugPrintThrottled(message);//Or another other custom code
};
Run Code Online (Sandbox Code Playgroud)


Cop*_*oad 6

printdebugPrint还有一些有一些字数限制,如果你有很长的东西要在控制台上打印,你可以:

创建这个方法:

void printWrapped(String text) {
  final pattern = RegExp('.{1,800}'); // 800 is the size of each chunk
  pattern.allMatches(text).forEach((match) => print(match.group(0)));
}
Run Code Online (Sandbox Code Playgroud)

用法:

printWrapped("Your very long string ...");
Run Code Online (Sandbox Code Playgroud)

来源


Jay*_*Jay 5

debugPrint()
Run Code Online (Sandbox Code Playgroud)

不妨使用而不是因为print()它试图减少 Android 内核上的日志行丢失或乱序

参考文献:

登录 Flutter