简体   繁体   中英

Flutter - removing duplicates from array

BOTTOM LINE: How can I retrieve data from firebase database, insert in array, and refresh a listview without getting duplicate items?

I am getting data from Firebase Database and loading it onto an array in a flutter application. Then, I use a Listview.Builder to make a list of the items of the array.

The thing is that when I refresh the page (using a refresh package), the items in the list get duplicated. They get loaded from firebase and onto the array again.

I have tried to use array.contains(element.id) (because every item has a unique ID) to conditionally insert the item into the array IF it is not already there.

However, this condition is always false, although the IDs are the same in the array (I printed to check). How can I get this condition, or something similar, to work?

Function to check duplicates:

bool isDuplicate(Group group) {
bool result = false;
if (groupList.contains(group.id)) {
  result = true;
} else {
  result = false;
}
return result;}

Function to get item from database:

Future searchGroupsDB() async {
return await userGroupsRef.once().then((snapshot1) {
  Map<dynamic, dynamic>? groupTitles = snapshot1.value;
  if (groupTitles != null) {
    groupTitles.forEach((key, value1) {
      userGroupsRef.child(key).once().then((snapshot2) {
        String? title = snapshot2.key;
        String id = snapshot2.value["ID"];
        String colors = snapshot2.value["Colors"];
        String description = snapshot2.value["Description"];
        if (snapshot2.value != null) {
          Group groupToInsert = Group(
              id: id,
              title: title!,
              description: description,
              containsImage: (displayImage != null) ? true : false,
              color: GradientColors.black,
              textColor: Colors.white);
          if (isDuplicate(groupToInsert) == true) {
            print("group already exists"); //this is never printed
          } else if (isDuplicate(groupToInsert) == false){
           groupList.insert(0, groupToInsert);
          }
        }
      });
    });
  }
});}

I appreciate the help in advance!

If you want to only have unique items in the array, consider using the arrayUnion operator that does precisely that. For more on this, see the Firebase documentation on updating an item in an array , and the FlutterFire documentation on the FieldValue.arrayUnion method .


Update (since you're using Realtime Database)

On Firebase's Realtime Database you'll not typically want to use arrays to store unique values. The idiomatic JSON data structure for a set of unique values is:

{
  "value1": true,
  "value2": true
}

The true here is just a dummy value. More important is that the items that you want to unique store are the keys in this structure, which means that they are unique by definition. The equivalent of the arrayUnion for Flutter now becomes a simple ref.update({ "value1": true }) .

I resolved it finally after 1 month of trying. Answer :

@override bool operator ==(other) {
return (other is Group)
    && other.id == id; }

I needed to do this so that flutter understood that two Groups were the same if their IDs were the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM