flutter_form_builder
flutter_form_builder copied to clipboard
Provide Streams for FormBuilderState
Environment
Package version: 7.7.0
Description
What you'd like to happen:
For example, the package connectivity_plus has a onConnectivityChange
stream. When you subscribe to it, you get the changes in real time.
_formKey.currentState!.onChange.listen((Map<String, FormBuilderFieldState<FormBuilderField, dynamic>> values) {
// do what you want to do with fields
});
This is especially useful in state managers, specifically Bloc. Here, from Flutterly's Bloc tutorial from 8:34, you can see how streams can be utilized in Bloc.
This will be a nice feature. Things to consider:
- This onChange should be apply only entire form, for single fields or for both?
- The strem should send the instantValue or savedValues? I think that should be instantValues
- Idea to implement: can be create a streamController on FormBuilder or FormBuilderField and send event each time that call onChange (on setInternalFieldValue or didChange). Is importante dispose this stream when dispose widget or remove field.
This onChange should be apply only entire form, for single fields or for both?
I think the entire form would be better. Implementing each field a stream would be a-okay, but also an overkill. Streaming the change of entire form would also cover the individual fields. So, anyone interested in subscribing the event change on one field might as well subscribe to the entire form stream and get its value.
The strem should send the instantValue or savedValues? I think that should be instantValues
I'm not quite much familiar with the underlying concept of this project. So I have one question: Can we save an invalid form? I ask this because there is a saveAndValidate
method, which implies that it is actually saving even though the form is invalid. So:
-
if the answer is yes, we can save invalid forms, then the stream should send saved values, not instant ones. Since FormBuilderState.fields is type of
Map<String, FormBuilderFieldState>
and FormBuilderFieldState has many methods to check errors such ashasError
anderrorText
, any dev can actually extract the errors with simplewhere
on theMap
. -
if the answer is no, we can't save invalid forms (which doesn't look so): then the stream should send instant ones or there should be two different streams for instant and saved values, like
onInstantChanged
ondonSaveChanged
.
Yes, can save invalid value
+1 for this request
need this too
This would be a useful feature
It's been a couple of months since I started working on this feature but the current API is so focal on event-driven approach[^1] that trying to implement stream-driven approach is such a hassle and I don't have time to take it on unfortunately. I've initially tried this on #1162, which is pretty much abandoned.
If the requesters are in a dire need, they can check out streaming_forms, a project that I started. It even has a web-based demo as well. It relies on stream-driven approach and raw material components. It is still in alpha phase, but can work on it in small steps if it shows further interest.
[^1]: It is not a terrible decision per se. It is a quite mature project. Async streams were not a thing in Dart back in the day, so the decision is sensible in its own regard.