Query collection and filter the results by field items - only list field items once

Database & APIs

Hi folks,

I'm building a fitness app, where I have a collection of documents, each document is an exercise with name, description... and muscle groups. What I want to achieve, is to query every documents, list it by muscle groups only, but list each muscle group item only once.

For example, I want to create a new exercise, lets say Bench Press, what will be in the Chest muscle group. To be able to add the exercise, I click on a button, what takes me to a new window, and there will be all the muscle groups listed, I select Chest.

When I go to the new window, I query the database, and I want to list the items in a listview. However I have around 15 muscle groups and 270 exercises, the ideal outcome is to listeach muscle groups once, in order by name.

How do I do this? See attached picture. I kind of don't know how to set this up.

Thank you for your help in advance!

What have you tried so far?

I've tried to query the collection, and to set up actions, but I can't add custom function or set the value properly.

I also have a custom fnction to filter the results after the query, I add the results to the button text but I'm getting errors.

import 'dart:convert';

import 'dart:math' as math;

import 'package:flutter/material.dart';

import 'package:google_fonts/google_fonts.dart';

import 'package:intl/intl.dart';

import 'package:timeago/timeago.dart' as timeago;

import '/flutter_flow/lat_lng.dart';

import '/flutter_flow/place.dart';

import '/flutter_flow/uploaded_file.dart';

import '/flutter_flow/custom_functions.dart';

import '/backend/backend.dart';

import 'package:cloud_firestore/cloud_firestore.dart';

import '/auth/firebase_auth/auth_util.dart';

String? getUniqueMuscleGroups() {

/// MODIFY CODE ONLY BELOW THIS LINE

List<String> getUniqueMuscleGroups(List<Map<String, dynamic>> documents) {

Set<String> muscleGroups = {};

for (var document in documents) {

if (document.containsKey('muscle_group')) {

muscleGroups.add(document['muscle_group']);

}

}

return muscleGroups.toList();

}

/// MODIFY CODE ONLY ABOVE THIS LINE

}

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