【问题标题】:Flutter - remove a Realtime Firebase Data onTap()Flutter - 删除实时 Firebase 数据 onTap()
【发布时间】:2020-11-01 06:53:57
【问题描述】:

我有一个 ListView,其中填充了存储在 Firebase 实时数据库中的同一用户的不同地址。点击特定项目时,我会显示一个警报对话框,请求确认删除地址。当用户同意删除地址时,我想从 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();
                  },
                ),
              ],
            );
          });
        }
  

【问题讨论】:

    标签: flutter dart firebase-realtime-database


    【解决方案1】:

    你在找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();
                             })
    

    【讨论】:

    • 我想删除用户点击的特定地址。地址节点由许多具有不同键的地址组成。我担心如果我使用上面的那个,它会删除我所有的地址。
    • 在这种情况下,您需要知道要删除的地址的密钥,然后将其插入链中.......reference().child('AllUsers').child(widget.userNumber).child('Address').child(keyOfTheSpecificAddressToRemove).remove()
    • 嘿@Frank van Puffelen,这篇文章很有帮助,但没有给出我的问题的确切答案。抱歉,我忘记给这个答案投票了。
    猜你喜欢
    • 2019-05-28
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    • 2022-08-05
    相关资源
    最近更新 更多