flutter_form_builder
flutter_form_builder copied to clipboard
Awaiting within valueTransformer
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?
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
@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.
Yeah, this would have been great if valueTranformer would return FutureOr
This still a issue?
@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;
}
},
@poweriton If you need
valueTransformerto beasync, 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