我正在调试一款应用程序,但需要实时了解某些值。我想知道是否有办法在控制台中打印消息,类似于使用Javascript的console.log。
感谢您的帮助。
在 import 'dart:developer'
库中有更多有用的方法,其中之一是 log()
。
例如:
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
阅读更多:
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()
或使用字符串插值,就像我在提供的示例片段中所演示的那样。
您可以使用
print()
函数或
debugPrint()
debugPrint()函数可以打印大量的输出。
import 'package:flutter/material.dart';
。 - colinFoo 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.*/
}
使用调试打印避免在生产应用中记录日志。
debugPrint("Message");
debugPrint = (String message, {int wrapWidth})
{
debugPrintThrottled(message);//Or another other custom code
};
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 ...");
关于使用字符串连接的另一个答案:
// 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
import 'dart:developer';
debugLog({String tag = '', required dynamic value}) {
log("TAG $tag : ${value.toString()}");
}
我认为这可能会对你有所帮助,因为我也曾陷入许多无法知道dart文件输出的方式,因此我通过按照视频中显示的步骤得到了解决方案。
array [0]
,array[1]
或任何运行时分配的代码? - Jack'dart:developer'
中的log
或debugPrint
。 - Dave Martorana