{

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


 Print class object instance properties into the console in dart or flutter

This tutorial shows multiple ways to print a class object in Dart and flutter

  • How to print Class objects
  • How to print Class object using toString() method
  • How to fully dump and print object variables to the console?
  • Print object properties of an instance
  • How to show data from a class
  • Print object variable into JSOn format.

The Dart class contains properties and functions, It is a blueprint for an instance of the object created.

When an error occurs while running an application, it is essential to output the dart object to the console so that the actual values can be inspected for further troubleshooting. When you use print() or logger statements to print an object, it displays format by default - 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. And it does not help the developer to inspect what type of data holds 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 you are accessing an instance variable of an object, the default toString() method returns the string and returns the Instance of ‘Employee’.

So, You override the toString() method in a class and print an object.

Let’s override the toString method in Employee.dart, Inside a method, Write a code to return the properties and values with your 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}

How to print class object instance into JSON format in the console

There are a lot of ways we can convert using JSOn libraries.

Here is a simple and easy approach to printing object properties in JSON format.

Add the toJson() method to the class Below is added to the console.

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

Import thedart:convert package that converts the types of data for encoding and decoding.

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}

Conclusion

Learn how to print Class instance objects into the console using the toString method and JSOn format.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not