Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 [cloud_firestore] Calling Query.aggregate(average('value')).get() on an empty collection throws an exception #12229

Closed
alextekartik opened this issue Jan 31, 2024 · 1 comment · Fixed by #12304
Assignees
Labels
platform: android Issues / PRs which are specifically for Android. plugin: cloud_firestore resolution: fixed A fix has been merged or is pending merge from a PR. type: bug Something isn't working

Comments

@alextekartik
Copy link
Contributor

alextekartik commented Jan 31, 2024

Bug report

This bug happens on Android and web (not tested on iOS). It works ok when there is at least one item but crashes if no item are present. sum and count works fine.

Steps to reproduce

Try the following on a collection (empty or not)

// OK
await coll.aggregate(count()).get();
// OK
await coll.aggregate(sum('value')).get();
// crashes on an empty collection. It should succeed and have a null value in the snapshot
await coll.aggregate(average('value')).get();

If the collection is not empty (and at least one record has a value field), it works fine, otherwise it throws a Platform exception

E/flutter (11444): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(firebase_firestore, null, {}, null)
E/flutter (11444): #0      FirebaseFirestoreHostApi.aggregateQuery (package:cloud_firestore_platform_interface/src/pigeon/messages.pigeon.dart:1172:7)
E/flutter (11444): <asynchronous suspension>
E/flutter (11444): #1      MethodChannelAggregateQuery.get (package:cloud_firestore_platform_interface/src/method_channel/method_channel_aggregate_query.dart:31:9)
E/flutter (11444): <asynchronous suspension>
E/flutter (11444): #2      AggregateQuery.get (package:cloud_firestore/src/aggregate_query.dart:24:37)
E/flutter (11444): <asynchronous suspension>

Expected behavior

While the average value or the resulting snapshot should be null (i.e. cannot get an average), the request should not crash.

Additional context

Tested on Android and Web. A similar request using the REST API or on node.js works fine (and reports a null average value)


Flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[!] Flutter (Channel stable, 3.16.9, on Ubuntu 22.04.3 LTS 6.5.0-15-generic,
    locale en_US.UTF-8)
    ! Warning: `dart` on your path resolves to
      /opt/app/dart/stable-3.2.6/dart-sdk/bin/dart, which is not inside your
      current Flutter SDK checkout at /opt/app/flutter/stable/flutter. Consider
      adding /opt/app/flutter/stable/flutter/bin to the front of your path.
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2023.1)
[✓] IntelliJ IDEA Ultimate Edition (version 2023.3)
[✓] VS Code (version unknown)
    ✗ Unable to determine VS Code version.
[✓] Connected device (3 available)
[✓] Network resources

Flutter dependencies

dependencies:
- cloud_firestore 4.14.0
- cloud_firestore_platform_interface 6.1.0
- cloud_firestore_web 3.9.0 
@alextekartik alextekartik added Needs Attention This issue needs maintainer attention. type: bug Something isn't working labels Jan 31, 2024
@darshankawar darshankawar added the triage Issue is currently being triaged. label Jan 31, 2024
@darshankawar
Copy link

darshankawar commented Jan 31, 2024

Thank for the report. I was able to replicate this on Android, wherein it throws below error:

I/flutter (32020): Instance of 'AggregateQuerySnapshot'
I/flutter (32020): Instance of 'AggregateQuerySnapshot'
E/flutter (32020): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(firebase_firestore, null, {}, null)
E/flutter (32020): #0      FirebaseFirestoreHostApi.aggregateQuery (package:cloud_firestore_platform_interface/src/pigeon/messages.pigeon.dart:1172:7)
E/flutter (32020): <asynchronous suspension>
E/flutter (32020): #1      MethodChannelAggregateQuery.get (package:cloud_firestore_platform_interface/src/method_channel/method_channel_aggregate_query.dart:31:9)
E/flutter (32020): <asynchronous suspension>
E/flutter (32020): #2      AggregateQuery.get (package:cloud_firestore/src/aggregate_query.dart:24:37)
E/flutter (32020): <asynchronous suspension>
E/flutter (32020): #3      _MyHomePageState._incrementCounter (package:aggregate_test/main.dart:81:23)
E/flutter (32020): <asynchronous suspension>
E/flutter (32020): 

On web I think it correctly throws the null error.

Error: TypeError: null: type 'Null' is not a subtype of type 'Object'
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 297:3  throw_
dart-sdk/lib/_internal/js_shared/lib/rti.dart 1463:3                         _asObject
dart-sdk/lib/collection/linked_hash_map.dart 187:26                          <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/linked_hash_map.dart 21:7      forEach
dart-sdk/lib/collection/linked_hash_map.dart 186:10                          from
packages/cloud_firestore_web/src/interop/firestore.dart 885:21               _fromJsObject
packages/cloud_firestore_web/src/interop/firestore.dart 880:32               getInstance
dart-sdk/lib/async/zone.dart 1661:54                                         runUnary
dart-sdk/lib/async/future_impl.dart 162:18                                   handleValue
dart-sdk/lib/async/future_impl.dart 838:44                                   handleValueCallback
dart-sdk/lib/async/future_impl.dart 867:13                                   _propagateToListeners
dart-sdk/lib/async/future_impl.dart 643:5                                    [_completeWithValue]
dart-sdk/lib/async/future_impl.dart 713:7                                    callback
dart-sdk/lib/async/schedule_microtask.dart 40:11                             _microtaskLoop
dart-sdk/lib/async/schedule_microtask.dart 49:5                              _startMicrotaskLoop
dart-sdk/lib/_internal/js_dev_runtime/patch/async_patch.dart 181:7           <fn>


Sample code:

final collection = db.collection('test');

final snapshot3 = await collection.aggregate(average('value')).get();
print(snapshot3);

I was unable to run for iOS as it gave me some error while running flutterfire configure, so adding android label for tracking.

@darshankawar darshankawar added plugin: cloud_firestore platform: android Issues / PRs which are specifically for Android. platform: web Issues / PRs which are specifically for web. and removed Needs Attention This issue needs maintainer attention. triage Issue is currently being triaged. platform: web Issues / PRs which are specifically for web. labels Jan 31, 2024
@Lyokone Lyokone self-assigned this Feb 2, 2024
@darshankawar darshankawar added the resolution: fixed A fix has been merged or is pending merge from a PR. label Feb 9, 2024
@firebase firebase locked and limited conversation to collaborators Mar 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform: android Issues / PRs which are specifically for Android. plugin: cloud_firestore resolution: fixed A fix has been merged or is pending merge from a PR. type: bug Something isn't working
Projects
None yet
4 participants