ayowhatthedogdoin
ayowhatthedogdoin

Reputation: 71

Flutter: child.ref.remove and children.length not counting properly

I'm new to Flutter and I have an app in which I want to remove and add books to my Firebase RealTime Database.

The problem is when I try to remove a book by clicking on an icon, the book is indeed removed from the Firebase, however, when I run some code further down, in which i need the quantity of books on the wishlist, it always counts 1 more than the real number.

Here's my code in which I remove the selected book:

// Which means the book was in the wishlist already, so we have to remove it
if (isBookWishb) {
  for (DataSnapshot element in userTable.children) {
    if (element.key == widget.userID) {
        if (element.child("BOOKS_WISH").exists) {
          for (int i = 0; i < element.child("BOOKS_WISH").children.length; i++) {
            if (element.child("BOOKS_WISH").child(i.toString()).value.toString() == widget.book.bookID) {
              element.child("BOOKS_WISH").child(i.toString()).ref.remove();
              break;
            }
          }
        }
        isBookWishb = false;
      break;
    }
  }
}

And this is how I retrieve the quantity of books a certain user has (on the same for loop, for example)

element.child("BOOKS_WISH").children.length

And here's my database here's my database

When I remove "2131313", it still counts the quantity correctly, same thing when I add it back. The problem occurs when I try to remove "56" or "6". If I remove one of these, the quantity after I do it still shows up as 3, when it should be 2, so when I add the book to the database again, it goes to 4 instead of 3

I've tried .set(null) instead, giving a few seconds to make sure I'm not overlapping the firebase process with my prints, but nothing seems to work. Because of this small thing, my whole project is ruined, and I can't seem to fix it. Thanks in advance for your attention

Upvotes: 2

Views: 250

Answers (1)

Jay
Jay

Reputation: 35659

That's a Firebase array!

Arrays are often difficult to use in NoSQL Databases (The Realtime Database in particular); you cannot insert or remove individual elements as the array is treated as one object, and the indexing will never change; e.g. value 2131313 is 'stuck' at index 2.

If you want to update an array, it has to be read, modified in code and re-written.

See this classic blog Arrays Are Evil.

A better option is to store each book with it's own firebase generated key. Then you can easily add/edit or remove.

So it would look like this

BOOKS_WISH
   -Y0kmos9j9h8fg: "56"
   -Y0joa90i90skl: "6"
   -Y0ziklklakldd: "2131313"

where -Y0kmos9j9h8fg etc are firebase generated keys

That structure also provides a lot more flexibility!

BOOKS_WISH
   -Y0kmos9j9h8fg:
       bookNum: "56"
       bookTitle: "Some title"
   -Y0joa90i90skl:
       bookNum: "6"
       bookTitle: "Another title"
   -Y0ziklklakldd:
       bookNum: "2131313"
       bookTitle: "Yet another title"

Now you can query, sort, etc on your book list. You could even add another node 'sort_index' to use for sorting

Upvotes: 1

Related Questions