I am using a provider to store the value of input fields in a form. On save() method the TextFields call a method on the FormInputProvider that updates the variable inputAnn that holds updated input values.
_inputAnn is listening to the variable inputAnn of the provider.
When I call the function _upDateAnnouncementProvider I am adding the Announcement _inputAnn to a List of Announcements, that contains the form input information. However the value of _inputAnn is not updated by the inputAnn provider.
I tried Therefore inserting an asynchronous function: The 2 print messages outside the _upDateAnnouncementProvider function are updated, but the print message inside the async function remains the old one.
Can anyone help? If necessary I can give more of the code.
Thanks.
Widget build(BuildContext context) {
var _inputAnn = Provider.of<FormInputProvider>(context).inputAnn;
print('FORM SCREEN BUILD BEFORE : images: ${_inputAnn.images.first}');
Future<void> _upDateAnnouncementProvider() async {
Future.delayed(Duration.zero).then((value) {
Provider.of<Announcements>(context, listen: false).addItem(_inputAnn);
print('INSIDE UPDATE PROVIDER: images: ${_inputAnn.images.first}');
});
}
print('FORM SCREEN BUILD: images: ${_inputAnn.images.first}');
loadHomeTypeList();
return Scaffold(
appBar: AppBar(
title: Text('Aggiungi Annuncio'),
),
body: Padding(
padding: EdgeInsets.all(16.0),
child: FormBody(
scrollController: _scrollController,
formKey: _formKey,
getFormFields: () => _getFormFields(context, _inputAnn),
saveForm: _saveForm,
updateProvider: _upDateAnnouncementProvider,
),
),
);
}
By passing the Provider Object to the function outside of the build, the code now works correctly.