I try to handle form with rxdart streams to check error on input text field and make decision based on combineLatest3 to submit or do not submit the form.
First field input value must be with length 3.
Second and third input fields are similar - there should be double value like 102.5 for example. But if input is 102,5 (notice the difference) then it is not a valid input.
Here are the rules based on stream transformers.
// `first`, `second` and `third` are BehaviourSubject<String> from rxdart
final validateFirst = StreamTransformer<String, String>.fromHandlers(
handleData: (first, sink) {
if (rapport.length != 3) {
sink.addError('first error');
} else {
sink.add(first);
}
},
);
final validateSecond = StreamTransformer<String, String>.fromHandlers(
handleData: (second, sink) {
try {
double.parse(second);
sink.add(second);
} catch (error) {
debugPrint('from second: ${error.toString()}');
sink.addError('second error');
}
},
);
final validateThird = StreamTransformer<String, String>.fromHandlers(
handleData: (third, sink) {
try {
double.parse(third);
sink.add(third);
} catch (error) {
debugPrint('from third: ${error.toString()}');
sink.addError('third error');
}
},
);
and here is a function which makes decision - are those inputs are valid.
// for some reason this gets true when there is an error in `second` stream
Stream<bool> get isValid => Rx.combineLatest3(first, second, third, (a, b, c) => true);
With following inputs, the form is valid.
100 (first input field)
12,5 (second input field)
120.4 (third input field)
with the above combination the isValid returns true but the second transformer clearly detect error, i.e. it outputs the following
I/flutter ( 3780): 12,5
I/flutter ( 3780): validate second: FormatException: Invalid double
Where is the error?
Simplifying the form to two fields (first and second), I noticed that the result of the isValid function (with the use of Rx.combineLatest2) is the last reaction to check in the corresponding transformer, for example, if you first enter the second field 12,4 (which is not suitable), and then enter the correct one in the first field value with a length of 3, then the form is considered valid - what does this mean?