我正在尝试使用 flutter 将数据保存到 firestore,棘手的部分是我的 textformfield 将数据设置为 null 或“”,因为它在表单页面加载时设置数据,然后我收到错误: Expected a value ot type '( () => void)?',但得到了“+Future”类型之一
下面是我的代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
我认为答案可能与未来某个地方的功能有关。
下面是我调用 addData 的地方:
FloatingActionButton.extended(
onPressed: addData(),
label: Text('Done'),
icon: Icon(Icons.add),
backgroundColor: Colors.blue,
),
Run Code Online (Sandbox Code Playgroud)