Reputation: 905
I have a ListView, filled with different addresses of same user stored at Firebase Realtime Database.on tap of specific item I show a alert dialog, requesting confirmation of deleting the address. I want to delete the address from firebase database when user agreed to delete it.
Here is Snippet of onTap of the specific item:
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();
},
),
],
);
});
}
Upvotes: 2
Views: 5705
Reputation: 599736
Are you looking for DatabaseReference.remove()
?
FirebaseDatabase.instance.reference()
.child('AllUsers')
.child(widget.userNumber)
.child('Address')
.remove();
You may want to only navigate away once the data has been removed from the database on the server, in which case you'd do":
FirebaseDatabase.instance.reference()
.child('AllUsers')
.child(widget.userNumber)
.child('Address')
.remove()
.then(() {
Navigator.of(context).pop();
})
Upvotes: 4