Get a parent reference from an app state

Actions & Logic

I have two collections, one ELO and one User. ELO is a sub-collection of User. In my User collection, I have a city property that determines the user's city. In my ELO collection, I have a sportName property that determines the sport associated with the ELO.

I want to retrieve the ELOs of a sport from users who have the same city as the logged-in user.

So I have a custom action that will retrieve the users in the same city and then retrieve the ELOs of those users. So far, so good.

My problem is that I want to display this as a leaderboard, but I can't do a reference query using the reference parent of the ELO, because in this case the reference parent doesn't exist. How can I obtain the parent reference from my ELO document?

This is my custom action and with the ELO in the return I want to have the users related to it :

// Automatic FlutterFlow imports
import '/backend/backend.dart';
import '/backend/schema/structs/index.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

Future<List<EloRecord>?> getEloCity(
  String? sport,
  String? city,
) async {
  // Get all users with the same city
  final QuerySnapshot userSnapshot = await FirebaseFirestore.instance
      .collection('users')
      .where('city', isEqualTo: city)
      .get();

  // Initialize a list to hold ELO records
  final List<EloRecord> eloRecords = [];

  // Loop through each user and get their ELO in the specified sport
  for (final DocumentSnapshot userDoc in userSnapshot.docs) {
    final QuerySnapshot eloSnapshot = await userDoc.reference
        .collection('ELO')
        .where('sportName', isEqualTo: sport)
        .get();

    // Add each found ELO record to the list
    for (final DocumentSnapshot eloDoc in eloSnapshot.docs) {
      print(EloRecord.fromSnapshot(eloDoc));
      eloRecords.add(EloRecord.fromSnapshot(eloDoc));
    }
  }

  // Sort the ELO records from highest to lowest
  eloRecords.sort((a, b) => b.points.compareTo(a.points));

  // Return the top 50 ELO records
  return eloRecords.take(50).toList();
}


What have you tried so far?

I tried to retrieve the parent with a custom function by doing a “.parent” on my ELO reference but it doesn't work.

Did you check FlutterFlow's Documentation for this topic?
Yes
2