Reputation: 93
I am trying to save data to firestore using flutter, the tricky part is my textformfield sets the data as null or "" because it sets the data as the form page loads in and then I get the error : Expected a value ot type '(() => void)?', but got one of type '+Future'
Below is my code:
final barcode = TextEditingController();
final price = TextEditingController();//these are from my textformfield
final stock = TextEditingController();
final color = TextEditingController();
addData() async {
// ASYNC HERE
Map<String, dynamic> theData = {
"Color": color.text,
"Price": price.text,
"Stock": stock.text,
};
CollectionReference collectionReference = Firestore.instance.collection("products");
await collectionReference.doc(barcode.text).set(theData);
}
I think the answer is probably something to do with a future function somewhere.
Below is where I call addData:
FloatingActionButton.extended(
onPressed: addData(),
label: Text('Done'),
icon: Icon(Icons.add),
backgroundColor: Colors.blue,
),
Upvotes: 3
Views: 5192
Reputation: 44220
You want onPressed: addData
(no parens) so that you're deferring the invocation of the code until the FAB is pressed. Instead you're invoking it immediately as you are setting up the FAB. Ouch.
Upvotes: 6