flutter_form_builder icon indicating copy to clipboard operation
flutter_form_builder copied to clipboard

Awaiting within valueTransformer

Open poweriton opened this issue 4 years ago • 6 comments

Im not sure if this is a bug or my lack of understanding:

I want to use the valueTransformer method on a signature pad asynchronously while i wait for the signature to upload, however the call doesn't seem to be waiting for the return url before returning the value on submit

 return FormBuilderSignaturePad(
          name: 'signature',
          controller: _controller,
          decoration: InputDecoration(labelText: "signature"),
          initialValue: _signatureFile?.readAsBytesSync(),
          onSaved: (newValue) async {},
          valueTransformer: (value) async {
            final savedUrl = await processSignature(value, context);
            return savedUrl;
          },
          onChanged: (value) {},
        );

the return type when calling SaveAndValidate or Save then Validate is a string representation of the future

if (_currentStep <= _totalSteps - 1) {
                    _formKey[_currentStep].currentState.save();

                    if (_formKey[_currentStep].currentState.validate()) {
                      var request = firestoreDatabase.currentRequest;
                      //Value is in this case "Future<String>" and not "http://urltoimage.co.uk"
                      var value = _formKey[_currentStep].currentState.value;

                      firestoreDatabase.updateRequest(request).then((value) {
                        if (_currentStep == _totalSteps - 1) {
                          //pop the screen
                          Navigator.pop(context);
                        } else {
                          setState(() {
                            _currentStep++;
                          });
                        }
                      });
                    }
                  }

any ideas if this is incorrectly programmed or a bug?

poweriton avatar Dec 18 '20 10:12 poweriton

It would be great to have valueTransformer as async. The problem, as it is, is that valueTransformer is called within Flutter's FormField.onSave which itself is synchronous and doesn't await

danvick avatar Dec 21 '20 10:12 danvick

@poweriton If you need valueTransformer to be async, then you need to find an alternative strategy to run your async logic... I recommend doing the async logic as part of the Form Submit logic, which could be async.

awhitford avatar Jan 08 '21 05:01 awhitford

Yeah, this would have been great if valueTranformer would return FutureOr

ebelevics avatar Oct 18 '21 00:10 ebelevics

This still a issue?

deandreamatias avatar Jun 09 '22 20:06 deandreamatias

@deandreamatias This is still an issue. valueTransformer does not await. This feature is necessary. For example, if I want to upload a MultipartFile. I've to do something as the followings.

valueTransformer: (images) async {
  if (images != null) {
    var multipartFile = [];
    for (final image in images) {
      multipartFile.add(await MultipartFile.fromFile(image.path));
    }
    return multipartFile;
  }
},

tonypottera24 avatar Jul 19 '22 13:07 tonypottera24

@poweriton If you need valueTransformer to be async, then you need to find an alternative strategy to run your async logic... I recommend doing the async logic as part of the Form Submit logic, which could be async.

@tonypottera24 this comment is useful to provide a solution to this. Why you need a MultipartFile in your form? A nice approve with SOLID principles, is you get a image type and transform in your state or data module

deandreamatias avatar Jul 19 '22 14:07 deandreamatias