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 i
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 json) {
name = json['name'];
gender = json['gender'];
age = json['age'];
}
Map toJson() {
final Map data = new Map();
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}