我正在调试应用程序,但是我需要即时了解一些值,我想知道是否有一种方法可以使用Javascript在console.log这样的控制台中打印消息。
感谢您的帮助。
答案 0 :(得分:8)
print()
可能正是您想要的。 Here's有关在flutter中进行调试的更多信息。
答案 1 :(得分:3)
我倾向于做类似的事情
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.*/
}
答案 2 :(得分:2)
您可以使用
print()
功能或
debugPrint()
debugPrint()函数可以打印大量输出。
答案 3 :(得分:2)
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 ...");
答案 4 :(得分:1)
RaisedButton(
child: Text("Click Me"),
onPressed: () {
print("This is a print()");
debugPrint("This is a debugPrint()");
},
),
您可以使用print()
或debugPrint()
方法将输出打印到控制台。
答案 5 :(得分:1)
使用调试打印来避免登录生产应用程序。
debugPrint("Message");
您还可以在main.dart或任何其他文件中禁用或更改调试打印实现:
debugPrint = (String message, {int wrapWidth})
{
debugPrintThrottled(message);//Or another other custom code
};
答案 6 :(得分:0)
import 'dart:developer'
库中还有更多有用的方法,其中之一是 log()
。
示例:
int i = 5;
log("Index number is: $i");
//output
[log] Index number is: 5
void log(字符串消息,{DateTime时间,int sequenceNumber,int级别 = 0,字符串名称='',区域区域,对象错误,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
可能的值 [name](可选)是日志消息来源的名称 [zone](可选)发出日志的区域 [错误](可选)与此日志事件关联的错误对象 [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()
或使用字符串插值,如我在提供的示例代码片段中所示。
答案 7 :(得分:0)
您可以在JavaScript中简单地使用与print('whatever you want to print')
相同的console.log()
。
有关更多信息,您可以check here。
答案 8 :(得分:0)
我认为这可能对您有所帮助,因为我在了解 dart 文件中代码的输出方面也遇到了很多困难,因此我按照视频中显示的步骤找到了解决方案。
答案 9 :(得分:-1)
与字符串连接的另一个答案:
// 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