如何将数据记录到Flutter控制台?

Abd*_*ؤمن 33 console intellij-idea dart flutter

你好Flutter Devs :)

我是初学者并使用IntelliJ IDEA,我想将数据记录到控制台?

我试过print()printDebug(),但没有我的数据被显示颤振控制台.

Abd*_*ؤمن 47

如果你在Flutter里面Widget,你可以使用debugPrint,例如,

import 'package:flutter/foundation.dart';

debugPrint('movieTitle: $movieTitle');
Run Code Online (Sandbox Code Playgroud)


否则,你可以使用Dar的log功能

import 'dart:developer';

log('data: $data');
Run Code Online (Sandbox Code Playgroud)

  • debugPrint 是否只打印一种调试模式?在我的应用程序中,我有很多打印语句,我使用 print() 来打印。我想做的是仅在调试模式下打印。 (8认同)
  • 请注意,debugPrint 现在包含在 flutter/material 中,因此您不一定需要导入 flutter/foundation。 (3认同)

Rah*_*nge 17

来自“飞镖:开发人员”的日志()

  • 它似乎没有像print()或那样的最大长度限制debugPrint()

  • 因此,当您想记录整个 API 响应时,它会很有帮助。

  • 并且还有助于在 dart 开发工具中显示格式化的日志记录。

import 'dart:developer';  //(auto import will do this even)
//example for api logging
  log("${response?.statusCode} :  ${response?.request?.path}",
          name: "Response", error: response.data);
Run Code Online (Sandbox Code Playgroud)

  • 它在网络应用程序中不起作用或我。 (3认同)

max*_*max 15

Dart print()函数输出到系统控制台,您可以使用浮动日志(基本上是adb logcat的包装器)查看该控制台。

如果您一次输出太多,则Android有时会丢弃一些日志行。为了避免这种情况,可以使用debugPrint()

在这里找到:https : //flutter.io/docs/testing/debugging


mik*_*n22 10

明确地说,debugPrint只能在 Flutter 小部件中工作。

  • 我想你可能是说,“要清楚地说明 `debugPrint` 只能在 Flutter 小部件中工作。”。 (5认同)
  • 我认为 @mikeyman22 可能想将其添加为评论,而不是答案 (3认同)

Hit*_*ahu 7

或者,我为 Flutter 应用程序创建了一个记录器: https: //github.com/hiteshsahu/Flutter-Logger

\n

只需复制AppLog.dart并添加到您的项目并像这样使用它:

\n
    \n
  1. AppLog.i("信息消息"); // 简单信息消息
  2. \n
  3. AppLog.i("主页", tag: "用户日志记录"); // 带有标识符TAG的信息消息
  4. \n
\n

例子:

\n

输入

\n
    AppLog.v("-----------------------------");\n    AppLog.d("I am Debug Log With Default TAG");\n    AppLog.i("I am Info Log With Default TAG");\n    AppLog.w("I am Warn Log With Default TAG");\n    AppLog.e("I am Error Log With Default TAG");\n    AppLog.wtf("I am Failure Log With Default TAG");\n    AppLog.v("I am Verbose Log With Default TAG");\n\n   //With TAGS\n    AppLog.v("-----------------------------");\n    AppLog.d("I am Debug Log With Custom TAG", tag: "Awesome Widget");\n    AppLog.i("I am Info Log With Custom TAG", tag: "Awesome Widget");\n    AppLog.w("I am Warn Log With Custom TAG", tag: "Awesome Widget");\n    AppLog.e("I am Error Log With Custom TAG", tag: "Awesome Widget");\n    AppLog.wtf("I am Failure Log With Custom TAG", tag: "Awesome Widget");\n    AppLog.v("I am Verbose Log With Custom TAG", tag: "Awesome Widget");\n    AppLog.v("-----------------------------");\n
Run Code Online (Sandbox Code Playgroud)\n

输出:

\n
\n
Restarted application in 347ms.\nFlutterApp: -----------------------------\nDEBUG|FlutterApp: I am Debug Log With Default TAG\nINFO\xe2\x93\x98|FlutterApp: I am Info Log With Default TAG\nWARN\xe2\x9a\xa0\xef\xb8\x8f|FlutterApp: I am Warn Log With Default TAG\nERROR\xe2\x9a\xa0\xef\xb8\x8f|\xef\xb8\x8fFlutterApp: I am Error Log With Default TAG\nWTF\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf|FlutterApp: I am Failure Log With Default TAG\nFlutterApp: I am Verbose Log With Default TAG\nFlutterApp: -----------------------------\nDEBUG|Awesome Widget: I am Debug Log With Custom TAG\nINFO\xe2\x93\x98|Awesome Widget: I am Info Log With Custom TAG\nWARN\xe2\x9a\xa0\xef\xb8\x8f|Awesome Widget: I am Warn Log With Custom TAG\nERROR\xe2\x9a\xa0\xef\xb8\x8f|\xef\xb8\x8fAwesome Widget: I am Error Log With Custom TAG\nWTF\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf|Awesome Widget: I am Failure Log With Custom TAG\nAwesome Widget: I am Verbose Log With Custom TAG\nFlutterApp: -----------------------------\n
Run Code Online (Sandbox Code Playgroud)\n
\n

您还可以设置日志级别的过滤器

\n
\n

AppLog.setLogLevel(log_priority); // log_priority 以下的日志将被隐藏

\n
\n

优先级始终是:

\n
\n

详细<=log_priority<=失败

\n
\n
\n

优先级:详细<调试<信息<警告<错误<失败

\n
\n

示例\n隐藏所有信息和调试日志:

\n
AppLog.setLogLevel(AppLog.WARN);\nAppLog.v("-----------------------------");\nAppLog.d("Debug Log Will not be Visible");\nAppLog.i("Info Log Will not be Visible");\nAppLog.w("Warn Log Will be Visible");\nAppLog.e("Error Log Will be Visible");\nAppLog.wtf("Failure Log Will be Visible");\nAppLog.v("Verbose Log  Will not be Visible");\nAppLog.v("-----------------------------");\n
Run Code Online (Sandbox Code Playgroud)\n

输出

\n
\n
WARN\xe2\x9a\xa0\xef\xb8\x8f|FlutterApp: Warn Log Will be Visible\nERROR\xe2\x9a\xa0\xef\xb8\x8f|\xef\xb8\x8fFlutterApp: Error Log Will be Visible\nWTF\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf|FlutterApp: Failure Log Will be Visible\n
Run Code Online (Sandbox Code Playgroud)\n
\n

请随意使用我的记录器,或者如果您有一些改进的想法,请创建 PR 或让我知道我会改进它。

\n