Sachin Saini
Β Β·Β FlutterFlow Expert

Firebase Aggregation Query Alternative using [Custom Code]

Hello, FlutterFlow community! πŸ‘‹

Are you looking to perform Firebase Aggregation Queries in FlutterFlow and wondering about alternatives? Well, you're in the right place! πŸš€

Problem:

As of now, FlutterFlow doesn't provide a direct way to execute Firebase Aggregation Queries, but fear not – where there's a will, there's a way!

Solution:

I have found a nifty workaround using Data Query and custom code. In this post, we'll walk you through the steps to achieve Firebase Aggregation-style queries in FlutterFlow.

You can also follow the Youtube Tutorial

Steps to Follow:

Step 1: Create a Custom Function: sumFirebaseQuery

Step 2: Define the Arguments: (List of double) amountRef

Step 3: Return Value: Double

Step 4: Copy And Paste the below code to Code Editor:

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';

double sumFirebaseQuery(List<double> amountRef) {
  double sum = 0.0;
  for (double amount in amountRef) {
    sum += amount;
  }
  return sum;
}

Step 5: Create a Page State (Double):

Step 6: On Page load Create two Actions:

a) Query the Collection: (Make sure to use the filters)

Make Sure to Name the Action Output

b) Update Page State: Set Value

Now, Get the value from the Custom Function

Quick Steps:

  1. Get the Values from the Action Variable

  2. Map List Item

  3. Under Map List item Select the Field which you wan to Sum.

Step 7: Display the Data from Page State in the Text Widget

That's All!

If you need any help in Integration you can Get help via 1:1 Session

20
22 replies