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

问题描述 投票:0回答:12

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

我很感谢您的帮助。

flutter dart debugging
12个回答
103
投票

print()
可能就是您正在寻找的。 这里有关在 flutter 中调试的更多信息。


39
投票

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

示例:

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

//output
[log] Index number is: 5

void log(String message, {DateTime 时间, int 序列号, int 级别 = 0,字符串名称 = '',区域区域,对象错误,StackTrace stackTrace})

发出日志事件。

该函数旨在与日志信息紧密映射 通过包收集: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

可能的值 [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);
  }
}

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

了解更多

请注意,只有

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


29
投票

你可以使用

print() 

功能或

debugPrint()

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


10
投票

我倾向于做类似的事情

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.*/
}

9
投票

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

debugPrint("Message");

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

debugPrint = (String message, {int wrapWidth}) 
{
    debugPrintThrottled(message);//Or another other custom code
};

6
投票

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

创建这个方法:

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)));
}

用途:

printWrapped("Your very long string ...");

来源


5
投票
debugPrint()

不妨使用而不是

print()
,因为它试图减少 Android 内核上的日志行丢失或故障

参考资料:

登录 Flutter


4
投票

与字符串连接的另一个答案:

// Declaration
int number = 10;


//Button Action
RaisedButton(
child: Text("Subtract Me"),
onPressed: () {
      number = number - 1;
      print('You have got $number as result');
      print('Before Value is ${number - 1} and After value is ${number + 1}');
    },
),

//Output:
flutter: You have got 9 as result
flutter: Before Value is 8 and After value is 10

2
投票

我用这样的东西。 print() 函数可以打印一定限制的数据。所以我用这个日志。

    import 'dart:developer';
    
    debugLog({String tag = '', required dynamic value}) {
      log("TAG $tag : ${value.toString()}");
    }

1
投票

您可以简单地使用

print('whatever you want to print')
,与 javascript 中的
console.log()
相同。

欲了解更多信息,您可以查看此处


1
投票

我认为这可能对您有帮助,因为,我也陷入了很多了解 dart 文件中代码输出的困境,因此我按照视频中所示的步骤得到了解决方案。

https://www.youtube.com/watch?v=hhP1tE-IHos

这里我展示了一个在观看视频后如何工作的实例。 检查左侧列,其中显示 profile 变量携带的值,即 null Dart dev tools working


0
投票

请注意,

print()
log()
选项都在行的开头添加自己的标签,并应用可能导致长行被截断的附加格式。对于
dart:io
应用程序,您可以通过直接转到 stdout/stderr 等来完全绕过此拦截和修改,如
stdout.write()
stdout.writeln()
等。同样,如果您希望显式登录到一个或其他。我在向 flutter 应用程序添加 CLI 参数时遇到了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.