Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
116
rated 0 times [  120] [ 4]  / answers: 1 / hits: 67367  / 7 Years ago, fri, february 24, 2017, 12:00:00

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



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



In JavaScript I would do one of the following:



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



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;
}


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.



So my question in detail:




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?



More From » dart

 Answers
0

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



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



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.



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.



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





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


[#58788] Wednesday, February 22, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janettejordynm

Total Points: 550
Total Questions: 94
Total Answers: 98

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
janettejordynm questions
Tue, Nov 24, 20, 00:00, 4 Years ago
Sat, May 23, 20, 00:00, 4 Years ago
Mon, Apr 6, 20, 00:00, 4 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
;