flutter_form_builder icon indicating copy to clipboard operation
flutter_form_builder copied to clipboard

Provide Streams for FormBuilderState

Open erayerdin opened this issue 2 years ago • 7 comments

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.

erayerdin avatar Nov 25 '22 11:11 erayerdin

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.

deandreamatias avatar Nov 27 '22 09:11 deandreamatias

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 as hasError and errorText, any dev can actually extract the errors with simple where on the Map.
  • 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 ond onSaveChanged.

erayerdin avatar Nov 27 '22 21:11 erayerdin

Yes, can save invalid value

deandreamatias avatar Dec 05 '22 06:12 deandreamatias

+1 for this request

wildsurfer avatar Feb 20 '23 15:02 wildsurfer

need this too

ozz-rjq avatar May 12 '23 13:05 ozz-rjq

This would be a useful feature

sharpsan avatar Apr 12 '24 15:04 sharpsan

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.

erayerdin avatar Apr 12 '24 21:04 erayerdin