I am trying to update my data in cloud Firestore through a form. I need the already stored fields in the firestore to be shown as initialValue in the TextFormField.
The fetched data is printed on the console by print() but it shows nothing in the TextFormField.
The initialValue has the same line of code I used for print().
The data is also fetched properly.
This is the code :
var initData = {
'title': '',
};
void didChangeDependencies() async{
super.didChangeDependencies();
await Firestore.instance
.collection('${widget.collection}')
.document('${widget.title}')
.get()
.then((value) {
setState(() {
initData = {
'title': value.data['title'],
};
});
});
if (isValid) {
_formKey.currentState
.save();
}
widget.submitFn(
_title,)
}
@override
Widget build(BuildContext context) {
print(initData['title']); // the fetched data is printed in the console
return Form(
key: _formKey,
Column(
children: <Widget>[
TextFormField(
initialValue: initData['title'], // this doesnt show anything on the TextFormField,
decoration: InputDecoration(
labelText: 'Enter Product Name',
contentPadding: EdgeInsets.all(5)),
validator: (value) {
if (value.isEmpty) {
return 'Enter a the Product Title';
}
return null;
},
onSaved: (value) {
_title = value;
},
),
),
),
}