How to get count and different string values from flutter firestore?

151

How can i create separate variables where all these options are captured and then their count?

Possible solutions depend on where you want to display the charts and how you want to calculate/prepare and store the data for these charts.

If you want to display the charts in your Flutter app

Storing the data in Firestore is a good approach if you want to display the charts in your Flutter app, because you can easily query the data with the Flutter plugin and you can apply Firestore Security Rules to the charts data the same way you secure the other app data.

You should however avoid querying all the Firestore documents each time you want to display a chart because this may rapidly increase your bill amount... A recommended approach is to maintain counters which hold the aggregated values. As explained in the doc: "Each counter is a document with a subcollection of shards, and the value of the counter is the sum of the value of the shards".

Since you save the data in one field, a good approach would be to use a Cloud Function that increments a set of counters.

More precisely, each time a doc is created/modified, the Cloud Function will read the "one field" value and, depending on whether it is color or height or width or flavour, update one (or more) counter(s). If the only dimension in your chart is the Category you need only 4 counters (color, height, width, flavor). If you want other dimensions (e.g. time or another category or specific values for the field, like different colors or different heights), you'll need to have more counters.

If you want to display the charts in another app/tool

If you need to display these charts in another app or tool, like a back-office application or a BI/Analytics tool like Google Data Studio or QlikView, there is an interesting alternative: You can use the Firebase Extension named "Export Collections to BigQuery", which allows exporting the documents in a Cloud Firestore collection to BigQuery, the serverless, highly scalable, enterprise data warehouse offered by Google Cloud.

When the data is in BigQuery you can use the SQL language to build SQL views and to prepare your data for visualisation. In particular aggregation queries are much easier, since you use SQL and you don't need to maintain Firestore counters (which, if you have a lot of dimensions, can become cumbersome).

I've recently written an article on how to configure this Extension which includes a simple example of an aggregation query.

Mixing the two approaches

If you want to display the charts in your Flutter app and your charts are really complicated (a lot of dimensions, i.e. many different colors, heights, ... aggregation by years, months, weeks, etc...) you could take advantages of the two approaches by:

  1. Exporting the data to BigQuery via the Extension
  2. Building some SQL views in BigQuery for chart data preparation
  3. Using a Cloud Function to export the chart data from BigQuery to Firestore documents
  4. Querying these Firestore documents from the Flutter app
Share:
151
akhicoda
Author by

akhicoda

Updated on December 31, 2022

Comments

  • akhicoda
    akhicoda over 1 year

    I have a document in Firestore where i have a category value/field. I am sending value to it through radio buttons on UI. So firestore will only have 1 string value for this field with total 4 possibility.

    UI- Color, Height, Width, Flavour Firestore - field= category, value = color or height or width or flavour

    Now i want to create charts with these values where all four options are shown as separate bars and their occurrences as frequency.

    I can get data from firestore using document snapshot. but not sure how to approach from here.

    How can i create separate variables where all these options are captured and then their count.

    Please help, thanks. I am using Flutter

    • Renaud Tarnec
      Renaud Tarnec over 2 years
      "i want to create charts with these values" => Are the charts to be displayed in your flutter app? Or through another app (e.g. a tool like Google Data Studio or QlikView)