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

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


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


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

To use it add:

import 'dart:developer';

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

inspect(myVar);


If it's a map then you can convert to JSON. First import convert package from flutter.

import 'dart:convert';

then convert to JSON and print

print(json.encode(yourMapVariable));

Update

Above answer is for map. For class / object add to toString() method.

Eg: say we have a user class

class User {  String name;  String address;  toString() {    return "name: " + name + ", address: " + address;  }}

You can auto generate this toString() method using your IDE.