gpt4 book ai didi

flutter - Flutter-删除实时Firebase数据onTap()

转载 作者:行者123 更新时间:2023-12-03 04:42:54 26 4
gpt4 key购买 nike

我有一个ListView,用存储在Firebase Realtime Database中的同一用户的不同地址填充。点击特定项目后,我显示一个警报对话框,要求确认删除该地址。当用户同意删除该地址时,我想从Firebase数据库中删除该地址。
这是特定项目的onTap的片段:

onTap: (){

if(widget.paymentFlag==true) {
if (addressList[index].Pin ==
widget.userPin) {
Navigator.push(
context, MaterialPageRoute(
builder: (context) =>
PaymentPage(
Pin: widget.userPin,)
));
}
else {
_scaffoldKey.currentState
.showSnackBar(
new SnackBar(
behavior: SnackBarBehavior
.floating,
backgroundColor: Colors.grey,
duration: new Duration(
seconds: 2),
content:
new Row(
children: <Widget>[

Icon(FontAwesomeIcons
.exclamation,
color: Colors.black,),
SizedBox(width: 10,),
new Text(
"Item not deliverable to this address",
style: TextStyle(
fontWeight: FontWeight
.w700,
fontStyle: FontStyle
.italic,
fontSize: 12,
color: Colors.black
),)
],
),
));
}
}else{
showDialog(
context: context,
builder: (BuildContext context) {
// return object of type Dialog
return AlertDialog(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
title: new Text("Remove this Address",style: TextStyle(fontSize: 20),),
content: new Text(addressList[index].Line1+"\n"+addressList[index].City+"\n"+addressList[index].Pin,style: TextStyle(fontSize: 16)),
actions: <Widget>[
// usually buttons at the bottom of the dialog
new FlatButton(
child: new Text("Close"),
onPressed: () {
Navigator.of(context).pop();
},
),
new FlatButton(
child: new Text("OK"),
onPressed: () {
FirebaseDatabase.instance.reference().child('AllUsers').child(widget.userNumber).child('Address');

///////////Here I want to delete the element///////////////////////////////////////////////

Navigator.of(context).pop();
},
),
],
);
});
}


最佳答案

您在寻找 DatabaseReference.remove() 吗?

FirebaseDatabase.instance.reference()
.child('AllUsers')
.child(widget.userNumber)
.child('Address')
.remove();
您可能只想导航一旦从服务器上的数据库中删除了数据,在这种情况下,您可以这样做:”:
FirebaseDatabase.instance.reference()
.child('AllUsers')
.child(widget.userNumber)
.child('Address')
.remove()
.then(() {
Navigator.of(context).pop();
})

关于flutter - Flutter-删除实时Firebase数据onTap(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62852790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com