flutter - 等到firestore中的数据成功更新
问题描述
当用户单击保存按钮并显示 toast 或要求用户在文档未更新时再试一次时,我想检查数据是否已成功上传到 firestore。
这是我更新文档的代码。
Future updateTechnical(Technical technical) async {
return await technicalsCollection.doc(technical.id).update({
"name": technical.name,
"address": technical.address,
"phoneNum": technical.phoneNum,
"tools": technical.tools,
});
}
这是保存按钮
ElevatedButton(
onPressed: () {
DatabaseService().updateTechnical(
Technical(
id: selectedTechnical.id,
name: nameController.text,
address: addressController.text,
phoneNum: phoneController.text,
tools: selected.join(','),
),
);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Save '),
Icon(Icons.person_add),
],
),
),
解决方案
要等到数据成功更新,您可以使用.then()
.
onPressed: () {
DatabaseService().updateTechnical().then((value) => {
print("successfully uploaded!"),
});
},
要检查上传过程中是否发生任何错误,您应该检查updateTechnical()
:
Future updateTechnical(Technical technical) async {
try {
final querySnapshot =
await technicalsCollection.doc(technical.id).update({
"name": technical.name,
"address": technical.address,
"phoneNum": technical.phoneNum,
"tools": technical.tools,
});
return querySnapshot;
} catch (error) {
print(error); //here you can see the error messages from firebase
throw 'failed';
}
}
如果您需要做某些事情,当发生特定错误时,不幸的是,您必须检查所有错误消息。这是一个示例:处理异常
推荐阅读
- java - Log4j 的 rollingPolicy.FileNamePattern 也在更改压缩文件的名称
- python - Discord bot 中的错误:Bot 没有属性“_default_help_command”
- r - 将向量传递给具有 R 中索引的 data.table
- python - 根据字典值过滤 DataFrame
- sql - 具有行号 sql 的新列
- java - Spring Boot Data 控制台应用程序
- node.js - Mongoose - 从精益中删除 _id (使用 toJSON() ?)
- javascript - 如何在 ko.js 中跟踪多个复选框的状态变化
- python - 有没有办法检查 python 中的 .xlsx 文件中的单元格是否突出显示(格式化为背景为黄色)?
- java - @Inheritance(strategy = InheritanceType.SINGLE_TABLE) 并按子属性过滤