Flutterfire: [cloud_firestore] orderBy not working if no data in firestore

Created on 2 Oct 2019  Â·  5Comments  Â·  Source: FirebaseExtended/flutterfire

Describe the bug
.orderBy() function only working if there is data match with firestore query.
If there is no data match with our query, library stuck and no return snapshot

Example data stored in firestore

{
    "status": "FINISHED",
    "user_id": "ABC",
    "finished_at": "2019-10-02T08:18:31.888987Z"
}

Example code

Future<List<UserCampaign>> getCampaignHistory(String userId) async {
    try {
      var campaignItems = List<UserCampaign>();
      QuerySnapshot qs = await Firestore.instance
          .collection('user_campaigns')
          .where('status', isEqualTo: 'FINISHED_') // we try to query with status == 'FINISHED_' , which is not found in our collections
          .where('user_id', isEqualTo: userId)
          .orderBy('finished_at', descending: true)
          .snapshots()
          .first;

      print('qs.documents ${qs.documents}'); // the code not even reach this line, until we comment .orderBy() function
      if (qs.documents.length > 0) {
        for (var ds in qs.documents) {
          print('ds.data ${ds.data}');
          if (ds.data != null)
            campaignItems.add(UserCampaign.fromJson(ds.data));
        }
        return campaignItems;
      }
      return null;
    } catch (err) {
      print('getCampaignHistory $err');
      return null;
    }
  }

Expected behavior
DocumentSnapshot returned with empty list

Additional context
cloud_firestore: ^0.12.9

cloud_firestore bug

Most helpful comment

This is a bug. If the query returns results then orderBy works as expected. If the query returns no results the snapshot connection state gets stuck on waiting.

All 5 comments

flutter doctor -v

[✓] Flutter (Channel stable, v1.9.1+hotfix.2, on Mac OS X 10.14.6 18G103, locale en-ID)
    • Flutter version 1.9.1+hotfix.2 at /Users/mychaelgo/flutter
    • Framework revision 2d2a1ffec9 (4 weeks ago), 2019-09-06 18:39:49 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0


[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/mychaelgo/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = /Users/mychaelgo/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.0)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.0, Build version 11A420a
    • CocoaPods version 1.7.5

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 39.0.3
    • Dart plugin version 191.8423
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] IntelliJ IDEA Ultimate Edition (version 2019.1)
    • IntelliJ at /Applications/IntelliJ IDEA.app
    • Flutter plugin version 35.2.2
    • Dart plugin version 191.6183.88

[✓] VS Code (version 1.38.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.4.1

[✓] Connected device (1 available)
    • iPhone 5s • FCA9FF7F-2A87-4794-A5EB-6CE5F411BC35 • ios • com.apple.CoreSimulator.SimRuntime.iOS-12-2 (simulator)

• No issues found!

Having the same issue here. When the result is empty no result return from stream.

This is a bug. If the query returns results then orderBy works as expected. If the query returns no results the snapshot connection state gets stuck on waiting.

Someone know a solution for this problem? Hanging on the same issue at the moment.

Fixed via https://github.com/FirebaseExtended/flutterfire/pull/2913

There was generally a bigger underlying issue here with errors not being caught with Streams (hence it looking like it hangs).

Was this page helpful?
0 / 5 - 0 ratings