Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fully dump / print variable to console in the Dart language?

Tags:

debugging

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.

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?

like image 360
Blackbam Avatar asked Feb 24 '17 13:02

Blackbam


People also ask

How do I print form data in console in flutter?

you can simply use print('whatever you want to print') same as console. log() in javascript.


1 Answers

dart:developer library includes inspect function that allows debuggers to open an inspector on the object.

To use it add:

import 'dart:developer'; 

And then you can see your inspected variable/object in console with:

inspect(myVar); 
like image 156
Paulo Belo Avatar answered Sep 28 '22 01:09

Paulo Belo