A toJson() method, which converts a User instance into a map.

With this approach, the calling code can have type safety,
autocompletion for the name and email fields, and compile-time exceptions.
If you make typos or treat the fields as ints instead of Strings,
the app won’t compile, instead of crashing at runtime.

user.dart

The responsibility of the decoding logic is now moved inside the model
itself. With this new approach, you can decode a user easily.

To encode a user, pass the User object to the jsonEncode() function.
You don’t need to call the toJson() method, since jsonEncode()
already does it for you.