【发布时间】:2021-03-26 10:45:57
【问题描述】:
我需要验证数据是否成功发送到Firebase,我有一个Field增量值,在用户的列表中,并且每次用户提交答案时该字段都会增加,但是问题是,即使答案是空的,如果我点击 addAnswer 按钮,增量正在发生,我希望仅在成功提交答案时更新增量计数器。
增量未来方法
Future answerCounter(BuildContext context, String userID,)async{
return FirebaseFirestore.instance.collection('users').doc(userID).update({
'answercounter': FieldValue.increment(1),
});
}
添加答案和答案计数器的代码
answerAdder(BuildContext context,
AsyncSnapshot<DocumentSnapshot> documentSnapshot, String QuesID) {
return showModalBottomSheet(
elevation: 0,
backgroundColor: Colors.transparent,
isScrollControlled: true,
context: context,
builder: (context) {
return Container(
height: MediaQuery.of(context).size.height * 0.8,
width: MediaQuery.of(context).size.width,
margin: EdgeInsets.symmetric(horizontal: 5),
decoration: BoxDecoration(
color: constantColors.white,
borderRadius: BorderRadius.only(
topRight: Radius.circular(25), topLeft: Radius.circular(25)),
),
child: Column(
children: [
Divider(
indent: 110,
endIndent: 110,
thickness: 4,
color: Colors.grey,
),
Padding(
padding: const EdgeInsets.all(5.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
MaterialButton(
onPressed: () {
Provider.of<PostFunctions>(context, listen: false)
.addAnswer(
context,
documentSnapshot.data.data()['question id'],
answerController.text,
titleController.text)
.whenComplete(() {
Navigator.pop(context);
print('adding counter');
if(documentSnapshot.hasData){
return Provider.of<PostFunctions>(context, listen: false)
.answerCounter(context, Provider.of<Authentication>(context,listen: false).getUserUid);
} else {
print('error');
}
});
Navigator.pop(context);
},
【问题讨论】:
标签: firebase flutter google-cloud-firestore