64

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?

Blackbam
  • 17,496
  • 26
  • 97
  • 150

8 Answers8

83

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);
Paulo Belo
  • 3,759
  • 1
  • 22
  • 20
26

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

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • 1
    DSON package works pretty nice for me if I try custom solution with reflection I will post it here thanks. – Blackbam Feb 24 '17 at 14:06
  • 2
    There are lots of questions with answers about reflection. Most of them should still work. Reflection didn't change that much. If you want to use reflection in production for web applications, there is also the `reflectable` package that transforms reflective code into static code to not hurt tree-shaking. – Günter Zöchbauer Feb 24 '17 at 14:08
25

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.

Saahithyan Vigneswaran
  • 6,841
  • 3
  • 35
  • 45
  • 7
    error `Uncaught Error: Converting object to an encodable object failed: Instance of 'User' ` – Aravin Aug 23 '20 at 11:23
  • @Aravin you have mistaken, you are trying to print an object. The answer above is for a `map`. If you have user `class`, better approach is to add a `toString()` method to it. – Saahithyan Vigneswaran Feb 17 '21 at 05:57
13

Simple little trick does the job

void printObject(Object object) {
  // Encode your object and then decode your object to Map variable
  Map jsonMapped = json.decode(json.encode(object)); 

  // Using JsonEncoder for spacing
  JsonEncoder encoder = new JsonEncoder.withIndent('  '); 

  // encode it to string
  String prettyPrint = encoder.convert(jsonMapped); 

  // print or debugPrint your object
  debugPrint(prettyPrint); 
}

// To use simply pass your object to it
printObject(yourObject); 
nick
  • 131
  • 1
  • 4
  • 1
    Explaining your code would increase the quality of your post. – Ruli Nov 13 '20 at 07:37
  • Wow so useful. I adjusted it a little but that encoder is exactly what I was looking for! Can finally read the logs easily! – Corné Sep 23 '21 at 04:37
  • 2
    fromJson() and toJson() have to be defined for the class for this to work. See the answer by @VeeyaaR – Polymath May 16 '22 at 14:30
5

Instance of 'FooBarObject' This happens when you try to directly do print(object);

Even if you use convert package from flutter, it would throw Uncaught Error: Converting object to an encodable object failed:

To resolve this issue, first, make sure your model class or data class contains fromJson and toJson methods.

If you do so, your model class would look like this.

class User {
  String name;
  String gender;
  int age;

  User({this.name, this.gender, this.age});

  User.fromJson(Map<String, dynamic> json) {
    name = json['name'];
    gender = json['gender'];
    age = json['age'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['name'] = this.name;
    data['gender'] = this.gender;
    data['age'] = this.age;
    return data;
  }
}

And to print this as json import convert package

import 'dart:convert';

And call json.encode

//after initializing the user object
 print('value is--> ' + json.encode(user);

Which will print the data as

value is--> {"name": "Vignesh","gender": "Male","age": 25}
VeeyaaR
  • 291
  • 4
  • 7
  • The `print('value is--> ' + json.encode(user);` statement has a syntax error →It is missing an close parens: `)`. – Victor Mar 31 '23 at 22:22
0

use await keyword with an object in async function void _registerUser() async { print(await form.result); }

SYED FAISAL
  • 495
  • 5
  • 8
0

If you want to know more about an instance of any class in an android studio click on a class name before variable then press ctrl+b it will take you to the class file where this object belongs from there you can see user attributes and methods of this class.

enter image description here

Red
  • 26,798
  • 7
  • 36
  • 58
SYED FAISAL
  • 495
  • 5
  • 8
-7

Try this one..

  void printWrapped(String text) {
    final pattern = new RegExp('.{1,800}'); // 800 is the size of each chunk
    pattern.allMatches(text).forEach((match) => print(match.group(0)));
  }
Hitesh sapra
  • 248
  • 2
  • 12
  • 3
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel Aug 02 '20 at 08:05