Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Immutability #10

Closed
noga-dev opened this issue Jun 22, 2024 · 2 comments · Fixed by #11
Closed

Immutability #10

noga-dev opened this issue Jun 22, 2024 · 2 comments · Fixed by #11
Assignees
Labels
enhancement New feature or request

Comments

@noga-dev
Copy link

Currently this is how my Teacher class is being generated:

class Teachers {
  String id;
  DateTime created_at;
  DateTime updated_at;
  String name;
  bool is_enabled;

  Teachers({
    required this.id,
    required this.created_at,
    required this.updated_at,
    required this.name,
    required this.is_enabled,
  });

  static String get table_name => 'teachers';
  static String get c_id => 'id';
  static String get c_created_at => 'created_at';
  static String get c_updated_at => 'updated_at';
  static String get c_name => 'name';
  static String get c_is_enabled => 'is_enabled';
  static Map<String, dynamic> insert({
    String? id,
    DateTime? created_at,
    DateTime? updated_at,
    required String name,
    bool? is_enabled,
  }) {
    return {
      if (id != null) 'id': id.toString(),
      if (created_at != null) 'created_at': created_at.toUtc().toString(),
      if (updated_at != null) 'updated_at': updated_at.toUtc().toString(),
      'name': name.toString(),
      if (is_enabled != null) 'is_enabled': is_enabled.toString(),
    };
  }

  static Map<String, dynamic> update({
    String? id,
    DateTime? created_at,
    DateTime? updated_at,
    String? name,
    bool? is_enabled,
  }) {
    return {
      if (id != null) 'id': id.toString(),
      if (created_at != null) 'created_at': created_at.toUtc().toString(),
      if (updated_at != null) 'updated_at': updated_at.toUtc().toString(),
      if (name != null) 'name': name.toString(),
      if (is_enabled != null) 'is_enabled': is_enabled.toString(),
    };
  }

  factory Teachers.fromJson(Map<String, dynamic> json) {
    return Teachers(
      id: json['id'] as String,
      created_at: DateTime.parse(json['created_at'].toString()),
      updated_at: DateTime.parse(json['updated_at'].toString()),
      name: json['name'] as String,
      is_enabled: json['is_enabled'] as bool,
    );
  }
}

Whereas it should be immutable by default:

class Teachers {
  final String id;
  final DateTime created_at;
  final DateTime updated_at;
  final String name;
  final bool is_enabled;

  const Teachers({
    required this.id,
    required this.created_at,
    required this.updated_at,
    required this.name,
    required this.is_enabled,
  });

  static String get table_name => 'teachers';
  static String get c_id => 'id';
  static String get c_created_at => 'created_at';
  static String get c_updated_at => 'updated_at';
  static String get c_name => 'name';
  static String get c_is_enabled => 'is_enabled';
  static Map<String, dynamic> insert({
    String? id,
    DateTime? created_at,
    DateTime? updated_at,
    required String name,
    bool? is_enabled,
  }) {
    return {
      if (id != null) 'id': id.toString(),
      if (created_at != null) 'created_at': created_at.toUtc().toString(),
      if (updated_at != null) 'updated_at': updated_at.toUtc().toString(),
      'name': name.toString(),
      if (is_enabled != null) 'is_enabled': is_enabled.toString(),
    };
  }

  static Map<String, dynamic> update({
    String? id,
    DateTime? created_at,
    DateTime? updated_at,
    String? name,
    bool? is_enabled,
  }) {
    return {
      if (id != null) 'id': id.toString(),
      if (created_at != null) 'created_at': created_at.toUtc().toString(),
      if (updated_at != null) 'updated_at': updated_at.toUtc().toString(),
      if (name != null) 'name': name.toString(),
      if (is_enabled != null) 'is_enabled': is_enabled.toString(),
    };
  }

  factory Teachers.fromJson(Map<String, dynamic> json) {
    return Teachers(
      id: json['id'] as String,
      created_at: DateTime.parse(json['created_at'].toString()),
      updated_at: DateTime.parse(json['updated_at'].toString()),
      name: json['name'] as String,
      is_enabled: json['is_enabled'] as bool,
    );
  }
}
@mmvergara mmvergara self-assigned this Jun 22, 2024
@mmvergara mmvergara added the enhancement New feature or request label Jun 22, 2024
@mmvergara mmvergara linked a pull request Jun 22, 2024 that will close this issue
@mmvergara mmvergara reopened this Jun 22, 2024
@mmvergara
Copy link
Owner

Would love to have your feedback on this

@noga-dev
Copy link
Author

lgtm 💪

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants