Different ways to print an object in Dart and Flutter| Dart By Example

This tutorial shows multiple ways for printing a class object in Dart and Flutter.

  • How to print Class objects
  • How to print a Class object using the toString() method
  • How to fully dump and print object variables to the console
  • Print object properties of an instance
  • How to display data from a class
  • Print object variables in JSON format

A Dart class contains properties and functions, serving as a blueprint for an object instance.

When troubleshooting application errors, it’s crucial to output Dart objects to the console to inspect actual values. Using print() or logger statements to print an object typically displays the default format: Instance of 'Class'.

Let’s declare a class for the print of an object.

Declared Employee class with properties name and salary properties. Created

class Employee {
  final String name;
  final int salary;

  Employee(this.name, this.salary);

}

final e1 = Employee('Erwin', 9000);
final e2 = Employee('Andrew', 70000);
final e3 = Employee('Mark', 8000);
final e4 = Employee('Otroc', 5000);

Let’s print the class object into the console using the print() function.

main() {
  print(e1.toString()); //Instance of 'Employee'
  print(e1); //Instance of 'Employee'
}

The output is shown in the console:

Instance of 'Employee'
Instance of 'Employee'

Here, printing an instance or instance.toString() prints Instance of ‘Employee’, It internally calls object.toString() and prints it. It does not help the developer to inspect what type of data is held in properties.

This post talks about multiple ways to display object content in Dart and Flutter.

How to Print a dart object to console in Dart

When accessing an instance variable of an object, the default toString() method returns the string Instance of 'Employee'.

To customize this behavior, you can override the toString() method in a class to print the desired representation of the object.

Let’s demonstrate by overriding the toString method in Employee.dart. Inside this method, write code to return the object’s properties and values in a preferred format:

 @override
  String toString() {
    return '{name: ${name}, salary: ${salary}}';
  }

Now, Print the object instance using the print() function.

main() {
  print(e1.toString()); //{name: Erwin, salary: 9000}
  print(e1); //{name: Erwin, salary: 9000}
}

Output:

{name: Erwin, salary: 9000}
{name: Erwin, salary: 9000}

Alternatively, you can use interpolation of an object using $obj.

main() {
  print($e1); //{name: Erwin, salary: 9000}
}

How to Print Class Object Instance in JSON Format in the Console

There are numerous ways to accomplish this using JSON libraries. Here, we present a simple and straightforward approach to print object properties in JSON format.

First, add the toJson() method to the class. Below is an example added to the console:

Below is added to the console.

  Map<String, dynamic> toJson() => {"name": name, "salary": salary};

Next, import the dart:convert package, which facilitates encoding and decoding different data types.

Here is a Complete example

import 'dart:convert';

class Employee {
  final String name;
  final int salary;

  Employee(this.name, this.salary);
  Map<String, dynamic> toJson() => {"name": name, "salary": salary};
}

final e1 = Employee('Erwin', 9000);

main() {
  print(e1.toJson()); // {name: Erwin, salary: 9000}
  print(jsonEncode(e1)); //{"name":"Erwin","salary":9000}
}

Output:

{name: Erwin, salary: 9000}
{"name":"Erwin","salary":9000}

jsonEncode is a function in the converted package. It allows to convert an object to a JSON-formatted string.

import 'dart:convert';

void main() {
  var emp = {"name": "Erwin", "salary": 9000};

  print(jsonEncode(emp));
}

If an object contains a JSON string, you can use the json.decode() function to convert the string to a JSON object. Then, convert the JSON object to a formatted object using jsonEncode.

import 'dart:convert';

void main() {
  //a JSON string object
  var emp = '{"name": "Erwin", "salary": 9000}';
  // decode to convert string to JSON
  var jsonObj = json.decode(emp);
// print formatted json using jsonEncode
  print(jsonEncode(jsonObj));
}

Conclusion

This tutorial covered how to print class instance objects to the console using the toString method and JSON format, providing insights into debugging and data representation in Dart and Flutter.