如何完全转储/打印变量以Dart语言进行控制台? [英] How to fully dump / print variable to console in the Dart language?

查看:82
本文介绍了如何完全转储/打印变量以Dart语言进行控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在寻找一个函数,该函数将Dart语言的动态变量尽可能完整地打印到控制台。

Hey there I am searching for a function which is printing a dynamic variable as completely as possible to the console in Dart language.

PHP var_dump()来获取有关变量的所有信息。

In PHP for instance I would use var_dump() in order to get all information about a variable.

JavaScript 我将执行以下操作之一:

In JavaScript I would do one of the following:

1)将对象转换为JSON并打印console.log(JSON.stringify(obj))

1) Convert Object to JSON and print console.log(JSON.stringify(obj))

2)或类似这样的自定义函数:

2) Or a custom function like this:

 function dump(arr,level) {
  var dumped_text = "";
  if(!level) level = 0;

  //The padding given at the beginning of the line.
  var level_padding = "";
  for(var j=0;j<level+1;j++) level_padding += "    ";

  if(typeof(arr) == 'object') { //Array/Hashes/Objects 
   for(var item in arr) {
    var value = arr[item];

    if(typeof(value) == 'object') { //If it is an array,
     dumped_text += level_padding + "'" + item + "' ...\n";
     dumped_text += dump(value,level+1);
    } else {
     dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
    }
   }
  } else { //Stings/Chars/Numbers etc.
   dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
  }
  return dumped_text;
 }

但是在 Dart 中,如果我< $ c> print(variable)我得到类似'FooBarObject'实例的东西。我不能像JavaScript中那样将对象转换为JSON,因为不可能对所有对象都这样。

However in Dart if I do print(variable) I get something like Instance of 'FooBarObject'. I cannot just convert an object to JSON like in JavaScript as this is not possible for all objects.

所以我的问题很详细:


是dart中的函数或自定义函数,可以打印未知类型(对象,数组等)的变量,包括所有(公共)属性以及嵌套对象和控制台的变量?还是最接近此功能的函数?

Is where a function or custom function in dart which can print a variable with unknown type (object, array, etc.) including all (public) properties as well as nested objects and variables to my console? Or which function is closest to this desire?


推荐答案

没有内置函数可以生成这样的输出。

There is no built in function that generates such an output.

print(variable)打印 variable.toString()'FooBarObject'实例。您可以在自定义类中覆盖它并打印其他内容。

print(variable) prints variable.toString() and Instance of 'FooBarObject' is the default implementation. You can override it in custom classes and print something different.

您可以使用反射( https://www.dartlang.org/articles/libraries/reflection-with-mirrors ),您可以自行构建一个功能来调查实例并以所需方式打印它。
您几乎可以做任何事情,出于调试目的,这绝对是个不错的选择。

You can use reflection (https://www.dartlang.org/articles/libraries/reflection-with-mirrors) to build a function yourself that investigates all kinds of properties of an instance and prints it the way you want. There is almost no limitation of what you can do and for debugging purposes it's definitely a fine option.

对于生产Web应用程序,应避免使用它,因为它严重限制了树状摇动,并将导致构建输出大小显着增加。
Flutter(移动设备)根本不支持反射。

For production web application it should be avoided because it limits tree-shaking seriously and will cause the build output size to increase notable. Flutter (mobile) doesn't support reflection at all.

您还可以使用其中一个JSON序列化程序包,从而可以轻松地向其中添加序列化自定义类,然后打印序列化的值。
例如

You can also use one of the JSON serialization packages, that make it easy to add serialization to custom classes and then print the serialized value. For example

  • https://pub.dartlang.org/packages/dson

我认为还有其他人,但是我不知道(缺点)优点,因为我通常使用 https://pub.dartlang .org / packages / source_gen

I think there are others, but I don't know about (dis)advantages, because I usually roll my own using https://pub.dartlang.org/packages/source_gen

这篇关于如何完全转储/打印变量以Dart语言进行控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆