Flutter: Firebase Realtime Remove object from a list of objects

后端 未结 1 955
挽巷
挽巷 2021-01-06 02:08

I am consulting all the clubs registered in the database. And for every club I add it to a list of objects.

When the person deletes the club deletes the club from th

相关标签:
1条回答
  • 2021-01-06 02:50

    Your problem lies on Club.

    When you create List<Club> items and later perform items.remove(clubInstance), internally the remove method will use the standard object equals method implementation, which is unaware of your key.

    The same would happen if you try to use items.indexOf(clubInstance). It will never 'find' the item, always returning -1.

    You can either change your implementation, iterating through the items to find out exactly which item you need to delete, and then remove it, OR you could implement == and hashCode in the Club class.

    If you intend to use club_name as the key, adding these two lines will probably solve it.

    class Club {
      Club({this.club_name});
      final String club_name;
    
      // this is what you would have to add to your class:
      bool operator ==(o) => o is Club && o.club_name == club_name;
      int get hashCode => club_name.hashCode;
    }
    

    Note: this is unrelated to flutter or firebase, this is just dart!

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