class User {
String name;
int age;
User(this.name, this.age);
Map toJson() => { // You must create this toJson() method else you will get an error
'name': name,
'age': age,
};
}
//Now in your main fucntion, use jsonEncode
import 'dart:convert';
main() {
User user = User('Shristi Gautam', 25);
String jsonUser = jsonEncode(user);
print(jsonUser);
}
The result will be something like {"name":"Shristi Gautam","age":25}