Junior — Middle
Can you explain what DTO means and its main purpose?
sobes.tech AI
Answer from AI
DTO (Data Transfer Object) is an object used for transferring data between application layers or systems. The main purpose of DTO is to simplify and optimize data transfer, often by aggregating multiple fields into a single object, as well as separating internal data models from external representations.
In Flutter, DTO is often used for exchanging data with a server, for example, when receiving a JSON response and converting it into an object that is convenient for use in the application.
Example of DTO in Dart:
class UserDTO {
final String id;
final String name;
UserDTO({required this.id, required this.name});
factory UserDTO.fromJson(Map<String, dynamic> json) {
return UserDTO(
id: json['id'],
name: json['name'],
);
}
Map<String, dynamic> toJson() {
return {
'id': id,
'name': name,
};
}
}
This object helps isolate data handling logic from its presentation and facilitates code maintenance and testing.