Skip to content

Commit 42b0048

Browse files
authored
Update README.md
1 parent f192b2d commit 42b0048

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,67 @@ Sample Mobile Validation using `rxdart` and `BLoC pattern`
1010
<img src="screenshots/demo.gif" height="480" alt="Cannot load image"/>
1111
</p>
1212

13+
# BLoC
14+
### 1. Create stream controllers to receive input: email, password, submit
15+
```dart
16+
// Stream controllers
17+
final emailS = BehaviorSubject.seeded('');
18+
final passwordS = BehaviorSubject.seeded('');
19+
final isLoadingS = BehaviorSubject.seeded(false);
20+
final submitLoginS = PublishSubject<void>();
21+
final subjects = [emailS, passwordS, isLoadingS, submitLoginS];
22+
```
23+
### 2. Map email text and password text to set of errors
24+
```dart
25+
// Email error and password error stream
26+
final emailError$ = emailS.map(validator.validateEmail).distinct().share();
27+
28+
final passwordError$ =
29+
passwordS.map(validator.validatePassword).distinct().share();
30+
```
31+
### 3. Combine email errors stream and password errors stream to valid stream
32+
```dart
33+
// Submit stream
34+
final submit$ = submitLoginS
35+
.throttleTime(const Duration(milliseconds: 500))
36+
.withLatestFrom<bool, bool>(
37+
Observable.combineLatest<Set<ValidationError>, bool>(
38+
[emailError$, passwordError$],
39+
(listOfSets) => listOfSets.every((errorsSet) => errorsSet.isEmpty),
40+
),
41+
(_, isValid) => isValid,
42+
)
43+
.share();
44+
```
45+
### 4. Perform login effect based on submit stream
46+
```dart
47+
// Message stream
48+
final message$ = Observable.merge(
49+
[
50+
submit$
51+
.where((isValid) => isValid)
52+
.withLatestFrom2(
53+
emailS,
54+
passwordS,
55+
(_, email, password) => Credential(
56+
email: email,
57+
password: password,
58+
),
59+
)
60+
.exhaustMap(
61+
(credential) => interactor.performLogin(
62+
credential,
63+
isLoadingS,
64+
),
65+
),
66+
submit$
67+
.where((isValid) => !isValid)
68+
.map((_) => const InvalidInformationMessage()),
69+
],
70+
).publish();
71+
```
72+
That's all :)
73+
1374
## Getting Started
1475

1576
For help getting started with Flutter, view our online

0 commit comments

Comments
 (0)