【问题标题】:How do I print to the console with Dart?如何使用 Dart 打印到控制台?
【发布时间】:2014-01-28 12:10:38
【问题描述】:

我希望我的 Dart 程序打印到浏览器的开发控制台。如何打印到控制台(例如 DevTools 的控制台)?

【问题讨论】:

    标签: dart


    【解决方案1】:

    使用print() 将字符串打印到浏览器的控制台:

    import 'dart:html';
    
    main() {
      var value = querySelector('input').value;
      print('The value of the input is: $value');
    }
    

    您将看到一条消息打印到开发者控制台。

    【讨论】:

    【解决方案2】:

    如果您只想将文本打印到控制台,您可以使用print('Text')

    但是,如果您想访问 DevTools 控制台的高级功能,您需要使用来自 dart:htmlConsole 类:Console.log('Text')

    它支持不同级别的打印(信息、警告、错误、调试)。它还允许打​​印表格和其他更高级的功能。请注意,并非每个浏览器都支持这些!很遗憾关于Console类的文档不完整,但是你可以看看Chrome的文档herehere

    【讨论】:

      【解决方案3】:

      如果您在这里是为了Flutter,那么您应该使用debugPrint

      这是相同的文档文本。

      /// Prints a message to the console, which you can access using the "flutter"
      /// tool's "logs" command ("flutter logs").
      
      /// 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.
      

      您可能会受到 SDK 版本限制,因为它仅适用于 2.2 及更高版本。

      【讨论】:

        【解决方案4】:

        还有来自import 'dart:developer' 库的log()

        示例:

        int name = "Something";
        log("ClassName: successfully initialized: $name");
        
        //output
        [log] ClassName: successfully initialized: Something 
        

        请注意,logdebugPrint 采用的 String 值与 print 不同。因此,您必须在最后添加 .toString() 或使用字符串插值,就像我在上面的示例中使用的那样。

        From doc:

        您有两个选项可用于记录您的应用程序。第一个是 使用stdoutstderr。通常,这是使用print() 完成的 语句,或通过导入 dart:io 并在 stderr 上调用方法和 stdout。例如:

        stderr.writeln('print me');

        如果你一次输出太多,那么 Android 有时会丢弃一些 日志行。为避免这种情况,请使用 Flutter 的 foundation 中的 debugPrint() 图书馆。这是print 的包装器,可将输出限制为 避免被 Android 内核丢弃的级别。

        应用程序日志记录的另一个选项是使用dart:developer log() 函数。这允许您包含更多的粒度和 日志输出中的信息。这是一个例子:

        import 'dart:developer' as developer;
        
        void main() {   
          developer.log('log me', name: 'my.app.category');
          developer.log('log me 1', name: 'my.other.category');
          developer.log('log me 2', name: 'my.other.category');
        }
        

        您还可以将应用程序数据传递给日志调用。该公约为 这是使用错误:log() 调用上的命名参数,JSON 对要发送的对象进行编码,并将编码后的字符串传递给 错误参数。

        import 'dart:convert'; import 'dart:developer' as developer;
        
        void main() {   
        
          var myCustomObject = ...;
        
          developer.log(
            'log me',
            name: 'my.app.category',
            error: jsonEncode(myCustomObject),   
          ); 
        }
        

        如果在 DevTool 的日志视图中查看日志输出,JSON 编码错误参数被解释为数据对象并呈现在 该日志条目的详细信息视图。

        read more(It's cool like a tutorial).

        【讨论】:

          【解决方案5】:

          Dart print() 函数在不同环境下的工作方式不同。

          • print() 在基于控制台的应用程序中使用时,它会在终端控制台中输出
          • print() 在基于 Web 的应用程序中使用时,它会输出到开发者控制台。
          void main() {
            print("HTML WebApp");
          }
          
          

          【讨论】:

            【解决方案6】:

            我知道 dartpad 支持的唯一方法是使用print(); 顺便说一下,Dart 使用 ${} 语法来表示表达式,或者只是 $ 表示单个值。 例如:-

            int x=3;
            print('hello world')  ;
            print(x) ;
            print('x = $x')  ;
            
            

            她是文档的链接 print method!

            【讨论】:

            猜你喜欢
            • 2018-12-15
            • 2021-06-27
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多