0
  • I have a fixed List i.e fixGuestList that is being initialized in init(). On a button tap, a list of Guests appears. On tapping the guest, the selected guest is being added to the fixed list i.e fixGuestList. So basically I'm adding elements from one list to the fixed list.

  • If the fixGuestList already contains the element present in the guestList. I want to pop up a snackBar() guest already added else add the element to fix list.

  • It is only working when the list is not been initialized. If I persist the list and on init() I assign the elements to the fixed list. And try to add duplicate guests the snackbar doesn't pop up it adds the duplicate guest to the list. For eg; I added guest 'A' from the guestList. And then I restarted the app and then again add the guest 'A' the guest 'A' is added even tho the fixedGuestList contains 'A' (I checked it in print statement 'A' exists) If I add 'A' and do not restart and try again try to add 'A' the snackbar pops up.

  • Getx is being used as StateManagement and GetStorage is used for Persisit data. The values of both list are maps.

Code are as follow:

  class GuestController extends GetxController {

  final _userDataController = Get.find<UserDataController>();


  List fixGuestList = List.filled(3, null, growable: false);
  List? guestList = _userDataController.userDataModel.value.totalGuest! // This list is filled via API

  // returns and empty index for fixGuestList
  int _returnIndex() {
    if (fixGuestList[0] == null) {
      return 0;
    } else if (fixGuestList[1] == null) {
      return 1;
    } else if (fixGuestList[2] == null) {
      return 2;
    }
    return 3;
  }

  // Add function
  void addToLane({required int index, required BuildContext context}) {
    if (fixGuestList.contains(guestList![index])) {
      ScaffoldMessenger.of(context).showSnackBar(_snackBar);
    } else {
      fixGuestList[_returnIndex()] = guestList![index];
      storageBox.write('FixGuestList', fixGuestList);
    }
    update();
  }

  //
  void onInitAssign() async {
    final _boxValue = storageBox.read('FixGuestList');

    if (_boxValue == null) {
      List fixGuestList = List.filled(3, null, growable: false);
    } else {
      fixGuestList = _boxValue;
    }
    update();
  }

  // OnInit Function
  @override
  void onInit() {
    onInitAssign();
    super.onInit();
  }
}

API Class:

class FirbaseAPI extends GetxController {
  
  
  
  @override
  void onInit() {
   userDataModel.bindStream(stream());
    super.onInit();
  }
  
   // Stream User Model
  Rx<UserDataModel> userDataModel = UserDataModel().obs;

  // Stream
  Stream<UserDataModel> stream() {
    return FirebaseFirestore.instance
        .collection('users')
        .doc('userA')
        .snapshots()
        .map((ds) {
      var mapData = ds.data();

      UserDataModel extractedModel = UserDataModel.fromMap(mapData);
      return extractedModel;
    });
  }
}

Model Class:

class UserDataModel {
  String? clubName;
  List? totalGuest;

  UserDataModel({
    this.clubName,
    this.totalGuest,
  });

  factory UserDataModel.fromMap(dynamic fieldData) {
    return UserDataModel(
      totalGuest: fieldData['totalGuest'],
    );
  }
}
Shakun's
  • 354
  • 4
  • 15

1 Answers1

1

contains uses operator equals (see here), commonly known as ==, to determine if something is equal.

To quote the documentation linked above:

The default behavior for all Objects is to return true if and only if this and other are the same object.

Override this method to specify a different equality relation on a class.

So whatever class it is you have there, I suggest you override the == operator, to make sure that two different instances of the same data still match. Otherwise you will see what you see now: only actual identical instances match.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • May you please tell me how can I use it in the existing code? @nvoigt – Shakun's Oct 19 '21 at 11:32
  • @Shakun's I cannot. You have not posted the class that is supposed to be compared. What does your class look like? If you post the code, I can try. – nvoigt Oct 19 '21 at 12:17
  • The whole code is in a single class i.e GuestController. Which class I'm missing? @nvoigt – Shakun's Oct 19 '21 at 12:43
  • The class that you want to compare. `guestList` is a list of *something*. You need to change that *something* to be comparable. I don't know what it looks like, you never posted it. – nvoigt Oct 19 '21 at 12:45
  • There is no other class. I want to compare elements of two Lists both in the same class. By class do you mean the class I'm calling the function ```addToLane()``` in? @nvoigt – Shakun's Oct 19 '21 at 17:50
  • No, I mean the class of the objects in the list. What kind of class does the API send you as a result? What do you want to save? – nvoigt Oct 19 '21 at 18:12
  • I have added the API class and Model Class. @nvoigt – Shakun's Oct 20 '21 at 09:10
  • Yes, but that is not the model class for the instances you want to compare. You want to compare two objects if they are the same (in this case that's needed to find out if it's already in another list). Please post the class definition of the instances *in your list*. What type is your list? Nothing is just "List". List of *what*? Why is it not a "List"? And if you don't want compiler help for it, at least *tell us* what Something is. – nvoigt Oct 20 '21 at 09:45