how delete a specific node in firebase?

前端 未结 1 866
耶瑟儿~
耶瑟儿~ 2021-01-28 05:20

I hooked up firebase and made the save data in it. But I can\'t delete a specific entry in firebase.

I tried deleting through method that I wrote below. But it doesn\'t

1条回答
  •  被撕碎了的回忆
    2021-01-28 06:10

    In order to delete a node, you need to know the complete path to that node. If you don't know that complete path, you can use a query to determine it.

    So in your case, if you know the phone number of the node you want to delete, you can query for all nodes with that phone number (since there may be multiple) and then delete them.

    The code for this would look something like:

    FirebaseDatabase.instance.reference()
      .child('customers')
      .orderByChild('phoneNumber')
      .equalTo('+79644054946')
      .onChildAdded.listen((Event event) {
        FirebaseDatabase.instance.reference()
          .child('customers')        
          .child(event.snapshot.key)
          .remove();
      }, onError: (Object o) {
        final DatabaseError error = o;
        print('Error: ${error.code} ${error.message}');
      });
    

    Update (20200221): You can also use a once() listener and loop over its child nodes with:

    FirebaseDatabase.instance.reference()
      .child('customers')
      .orderByChild('phoneNumber')
      .equalTo('+79644054946')
      .once()
      .then((DataSnapshot snapshot) {
        Map children = snapshot.value;
        children.forEach((key, value) {
          FirebaseDatabase.instance.reference()
            .child('customers')        
            .child(key)
            .remove();
        })
      });
    

    0 讨论(0)
提交回复
热议问题