0

I’m creating an app where I need to match two random users from my Firebase Database. My issue is that I'm not sure how to retrieve the list of users actively searching for a match. Here's how my database structure looks like:

Firebase Realtime Database

I'm looking to read the list of users searching for a match and compare match preferences to randomly match 2 users together. any suggestions on the syntax I should use to retrieve the list from Firebase Realtime Database to build the random matching logic?

1 Answer 1

1

try this:

UserModel getRandomUser(UserModel currentUser, List<UserModel> activelySearching) {
  final matchedUsers = activelySearching.where(
    (otherUser) {
      return currentUser.uid != otherUser.uid &&
      currentUser.genderPreference == otherUser.userGender &&
      currentUser.userGender == otherUser.genderPreference &&

      currentUser.maxAgePreference >= otherUser.userAge &&
      currentUser.minAgePreference <= otherUser.userAge &&

      currentUser.userAge <= otherUser.maxAgePreference &&
      currentUser.userAge >= otherUser.minAgePreference &&
    }
  ).toList();

  // import 'dart:math'; to use Random
  final index = Random().nextInt(matchedUsers.length);
  return matchedUsers[index];
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @mohamed.oussous for the tip there! How should I define UserModel? I have tried your suggestion and got an error that UserModel is not a defined class.
of course you have to define it yourself by parsing the JSON data to a UserModel
this video also helped on parsing the JSON data to a UserModel youtube.com/watch?v=sXBJZD0fBa4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.