Skip to content

Commit 4027f16

Browse files
committed
Add Part 1
0 parents  commit 4027f16

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

Part 1/user.dart

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class User {
2+
final String _id;
3+
final String _name;
4+
5+
String get id => _id;
6+
String get name => _name;
7+
8+
User._({String id, String name})
9+
: _id = id,
10+
_name = name;
11+
12+
factory User([void Function(UserBuilder) updates]) =>
13+
(UserBuilder()..update(updates)).build();
14+
15+
User rebuild(void Function(UserBuilder) updates) =>
16+
(toBuilder()..update(updates)).build();
17+
18+
UserBuilder toBuilder() => UserBuilder()..replace(this);
19+
20+
@override
21+
bool operator ==(Object other) {
22+
if (identical(other, this)) return true;
23+
return other is User && id == other.id && name == other.name;
24+
}
25+
26+
@override
27+
int get hashCode => id.hashCode ^ name.hashCode;
28+
29+
@override
30+
String toString() => '''User {
31+
id=$id,
32+
name=$name,
33+
}''';
34+
}
35+
36+
class UserBuilder {
37+
User _user;
38+
39+
String _id;
40+
String get id => _user != null ? _user._id : _id;
41+
set id(String id) => _this._id = id;
42+
43+
String _name;
44+
String get name => _user != null ? _user._name : _name;
45+
set name(String name) => _this._name = name;
46+
47+
UserBuilder get _this {
48+
if (_user != null) {
49+
_id = _user.id;
50+
_name = _user.name;
51+
_user = null;
52+
}
53+
return this;
54+
}
55+
56+
UserBuilder();
57+
58+
void replace(User other) {
59+
if (other == null) {
60+
throw ArgumentError.notNull('other');
61+
}
62+
_user = other;
63+
}
64+
65+
void update(void Function(UserBuilder) updates) {
66+
if (updates != null) updates(this);
67+
}
68+
69+
User build() {
70+
final result = _user ?? User._(id: id, name: name);
71+
replace(result);
72+
_this;
73+
return result;
74+
}
75+
}

README.MD

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Built Value and Built Collections Example
2+
3+
Repositorio de la serie de vídeos donde te enseño a dominar el paquete [built_value](https://github.com/google/built_value.dart).
4+
5+
# Tutoriales de YouTube
6+
7+
- [Parte 1 - Tipos de Valor Inmutables (Inmutable Value Types)](https://www.youtube.com/watch?v=oVNP1Kx4irw)
8+

0 commit comments

Comments
 (0)