While using order by clause the stream builder is not updating the data. But if I remove the order by clause then stream builder shows updated data.
To Reproduce
StreamBuilder(
stream: Firestore.instance.collection('orders').where(
'userUID', isEqualTo: futureSnapshot.data.uid).where('orderStatus', whereIn: allOrderStatus).orderBy('placedAtTime', descending: true).snapshots(),
builder: (context, snapshot) {
Expected behavior
On CRUD operations, the stream builder should update but it is not updating. But once I remove the order by then it works fine.
Hi @swapnilshah09
can you please provide your flutter doctor -v and flutter run --verbose? Also, to better address the issue, would be helpful
if you could post a minimal code sample to reproduce the problem
Thank you
flutter doctor -v:
[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.15.3 19D76, locale en-IN)
• Flutter version 1.12.13+hotfix.8 at /Users/swapnil/Documents/flutter
• Framework revision 0b8abb4724 (7 weeks ago), 2020-02-11 11:44:36 -0800
• Engine revision e1e6ced81d
• Dart version 2.7.0
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at /Users/swapnil/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-29, build-tools 29.0.2
• 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.4)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.4, Build version 11E146
• CocoaPods version 1.8.4
[✓] Android Studio (version 3.5)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 43.0.1
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
[✓] Connected device (1 available)
• iPhone 11 Pro Max • 7A2DCDBB-F6F1-4D1F-93CE-213F650CD6AF • ios • com.apple.CoreSimulator.SimRuntime.iOS-13-4 (simulator)
• No issues found!
flutter run --verbose:
localhost Runner[17221]: (GoogleUtilities) 6.14.0 - [Firebase/Firestore][I-FST000001] Listen for query at orders failed: The query requires
an index. You can create it here:
https://console.firebase.google.com/v1/r/project/pawfect-xyz/firestore/indexes?create_composite=Ckpwcm9qZWN0cy9wYXdmZWN0LXh5ei9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvb3JkZXJzL2luZGV4ZXMvX
xABGg8KC29yZGVyU3RhdHVzEAEaCwoHdXNlclVJRBABGhAKDHBsYWNlZEF0VGltZRACGgwKCF9fbmFtZV9fEAI
[+4199 ms] [DEVICE LOG] 2020-03-31 12:32:49.312408+0530 localhost Runner[17221]: (GoogleUtilities) 6.14.0 - [Firebase/Firestore][I-FST000001] Listen for query at orders failed: The query requires
an index. You can create it here:
https://console.firebase.google.com/v1/r/project/pawfect-xyz/firestore/indexes?create_composite=Ckpwcm9qZWN0cy9wYXdmZWN0LXh5ei9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvb3JkZXJzL2luZGV4ZXMvX
xABGg8KC29yZGVyU3RhdHVzEAEaCwoHdXNlclVJRBABGhAKDHBsYWNlZEF0VGltZRACGgwKCF9fbmFtZV9fEAI
[+1649 ms] [DEVICE LOG] 2020-03-31 12:32:50.962131+0530 localhost Runner[17221]: (GoogleUtilities) 6.14.0 - [Firebase/Firestore][I-FST000001] Listen for query at orders failed: The query requires
an index. You can create it here:
https://console.firebase.google.com/v1/r/project/pawfect-xyz/firestore/indexes?create_composite=Ckpwcm9qZWN0cy9wYXdmZWN0LXh5ei9kYXRhYmFzZXMvKGRlZmF1bHQpL2NvbGxlY3Rpb25Hcm91cHMvb3JkZXJzL2luZGV4ZXMvX
xABGg8KC29yZGVyU3RhdHVzEAEaCwoHdXNlclVJRBABGhAKDHBsYWNlZEF0VGltZRACGgwKCF9fbmFtZV9fEAI
[+34853 ms] [DEVICE LOG] 2020-03-31 12:33:25.814806+0530 localhost Runner[17221]: (CFNetwork) [com.apple.CFNetwork:Coalescing] success removing entry for host firebaselogging-pa.googleapis.com
config 0x6000031ba200
[ +1 ms] [DEVICE LOG] 2020-03-31 12:33:25.815149+0530 localhost Runner[17221]: (CFNetwork) Connection 7: is being canceled
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815252+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] [C7 80E3BA85-5647-426F-84C9-09270AACAA98
firebaselogging-pa.googleapis.com:443 tcp, url hash: 869fcef3, tls] cancel
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815443+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] [C7 80E3BA85-5647-426F-84C9-09270AACAA98
firebaselogging-pa.googleapis.com:443 tcp, url hash: 869fcef3, tls] cancelled
[ ] [DEVICE LOG] [C7.1 CC22EC3C-76CE-40D1-9123-4756059AA176 192.168.1.106:64350<->172.217.27.202:443]
[ ] [DEVICE LOG] Connected Path: satisfied (Path is satisfied), interface: en0
[ ] [DEVICE LOG] Duration: 120.933s, DNS @0.000s took 0.003s, TCP @0.004s took 0.028s, TLS took 0.091s
[ ] [DEVICE LOG] bytes in/out: 4464/1513, packets in/out: 9/10, rtt: 0.021s, retransmitted packets: 0, out-of-order packets: 0
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815496+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.000s [C7 647C88AC-54DA-4F64-8EFA-F9D6AA38E4E3
firebaselogging-pa.googleapis.com:443 resolver path=satisfied (Path is satisfied), interface: en0] path:start
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815537+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.000s [C7 647C88AC-54DA-4F64-8EFA-F9D6AA38E4E3
firebaselogging-pa.googleapis.com:443 resolver path=satisfied (Path is satisfied), interface: en0] path:satisfied
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815576+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.000s [C7 647C88AC-54DA-4F64-8EFA-F9D6AA38E4E3
firebaselogging-pa.googleapis.com:443 resolver path=satisfied (Path is satisfied), interface: en0] resolver:start_dns
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815625+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.003s [C7 647C88AC-54DA-4F64-8EFA-F9D6AA38E4E3
firebaselogging-pa.googleapis.com:443 resolver path=satisfied (Path is satisfied), interface: en0] resolver:receive_dns
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815669+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.003s [C7.1 CC22EC3C-76CE-40D1-9123-4756059AA176
192.168.1.106:64350<->172.217.27.202:443 socket-flow path=satisfied (Path is satisfied), interface: en0] path:start
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815706+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.003s [C7.1 CC22EC3C-76CE-40D1-9123-4756059AA176
192.168.1.106:64350<->172.217.27.202:443 socket-flow path=satisfied (Path is satisfied), interface: en0] path:satisfied
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815744+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.004s [C7.1 CC22EC3C-76CE-40D1-9123-4756059AA176
192.168.1.106:64350<->172.217.27.202:443 socket-flow path=satisfied (Path is satisfied), interface: en0] flow:start_connect
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815780+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.032s [C7.1 CC22EC3C-76CE-40D1-9123-4756059AA176
192.168.1.106:64350<->172.217.27.202:443 socket-flow path=satisfied (Path is satisfied), interface: en0] flow:finish_transport
[ ] [DEVICE LOG] 2020-03-31 12:33:25.815971+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.032s [C7 647C88AC-54DA-4F64-8EFA-F9D6AA38E4E3
firebaselogging-pa.googleapis.com:443 resolver path=satisfied (Path is satisfied), interface: en0] flow:finish_transport
[ +6 ms] [DEVICE LOG] 2020-03-31 12:33:25.816015+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.123s [C7.1 CC22EC3C-76CE-40D1-9123-4756059AA176
192.168.1.106:64350<->172.217.27.202:443 socket-flow path=satisfied (Path is satisfied), interface: en0] flow:finish_connect
[ ] [DEVICE LOG] 2020-03-31 12:33:25.816046+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.124s [C7 647C88AC-54DA-4F64-8EFA-F9D6AA38E4E3
firebaselogging-pa.googleapis.com:443 resolver path=satisfied (Path is satisfied), interface: en0] flow:finish_connect
[ ] [DEVICE LOG] 2020-03-31 12:33:25.816498+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.124s [C7.1 CC22EC3C-76CE-40D1-9123-4756059AA176
192.168.1.106:64350<->172.217.27.202:443 socket-flow path=satisfied (Path is satisfied), interface: en0] flow:changed_viability
[ ] [DEVICE LOG] 2020-03-31 12:33:25.816543+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 0.124s [C7 647C88AC-54DA-4F64-8EFA-F9D6AA38E4E3
firebaselogging-pa.googleapis.com:443 resolver path=satisfied (Path is satisfied), interface: en0] flow:changed_viability
[ ] [DEVICE LOG] 2020-03-31 12:33:25.816570+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] 120.933s [C7] path:cancel
[ ] [C7.1 CC22EC3C-76CE-40D1-9123-4756059AA176 192.168.1.106:64350<->172.217.27.202:443]
[ ] Connected Path: satisfied (Path is satisfied), interface: en0
[ ] Duration: 120.933s, DNS @0.000s took 0.003s, TCP @0.004s took 0.028s, TLS took 0.091s
[ ] bytes in/out: 4464/1513, packets in/out: 9/10, rtt: 0.021s, retransmitted packets: 0, out-of-order packets: 0
[ ] [DEVICE LOG] 2020-03-31 12:33:25.817483+0530 localhost Runner[17221]: (libboringssl.dylib) [com.apple.network.boringssl:BoringSSL] boringssl_context_message_handler(2258)
[C7.1:2][0x7fc7e100bff0] Writing SSL3_RT_ALERT 2 bytes
[ ] [DEVICE LOG] 2020-03-31 12:33:25.817652+0530 localhost Runner[17221]: (libboringssl.dylib) [com.apple.network.boringssl:BoringSSL] boringssl_context_handle_warning_alert(1893)
[C7.1:2][0x7fc7e100bff0] write alert, level: warning, description: close notify
[ ] [DEVICE LOG] 2020-03-31 12:33:25.817740+0530 localhost Runner[17221]: (libboringssl.dylib) [com.apple.network.boringssl:BoringSSL] boringssl_session_disconnect(504)
[C7.1:2][0x7fc7e100bff0] SSL_shutdown 0
[ ] [DEVICE LOG] 2020-03-31 12:33:25.817895+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] nw_flow_disconnected [C7.1 172.217.27.202:443 cancelled socket-flow
((null))] Output protocol disconnected
[ ] [DEVICE LOG] 2020-03-31 12:33:25.818123+0530 localhost Runner[17221]: (libnetwork.dylib) [com.apple.network:] nw_connection_report_state_with_handler_on_nw_queue [C7] reporting state
cancelled
[ ] [DEVICE LOG] 2020-03-31 12:33:25.818329+0530 localhost Runner[17221]: (CFNetwork) Connection 7: destroyed
[ ] [DEVICE LOG] 2020-03-31 12:33:25.818549+0530 localhost Runner[17221]: (libboringssl.dylib) [com.apple.network.boringssl:BoringSSL] nw_protocol_boringssl_remove_input_handler(1012)
[C7.1:2][0x7fc7e100bff0] nw_protocol_boringssl_remove_input_handler forced true
[ ] [DEVICE LOG] 2020-03-31 12:33:25.818974+0530 localhost Runner[17221]: (libboringssl.dylib) [com.apple.network.boringssl:BoringSSL] nw_protocol_boringssl_remove_input_handler(1030)
[C7.1:2][0x7fc7e100bff0] Transferring nw_protocol_boringssl_t handle back into ARC for autorelease
I have already provided a minimal code sample of how I am using stream builder and how order by is used:
StreamBuilder(
stream: Firestore.instance.collection('orders').where(
'userUID', isEqualTo: futureSnapshot.data.uid).where('orderStatus', whereIn: allOrderStatus).orderBy('placedAtTime', descending: true).snapshots(),
builder: (context, snapshot) {
Hey there, when chaining .where() and .orderBy() with different field values, you have manually create indexes in Firestore, Refer to this solution. https://github.com/flutter/flutter/issues/15928#issuecomment-394197426
Thanks, @healer2030, Composite indexing worked but still, there are two issues:
@swapnilshah09 when you did the composite indexing, were the field paths in the following order: userUID, orderStatus, placedAtTime?
Also ensure each field path is set to descending as your code above suggests it needs to be descending.
Even after creating composite indexing, I get empty response from streambuilder. For creating composite index I use the link generated in flutter console so there is no problem atleast in the composite index
@healer2030
I am facing the same issue that @psbankar is facing. It is updating the stream builder when a new entry is made but it is not updating the stream builder when there are any update operations performed on the data.
Hey @swapnilshah09 and @psbankar, please reply with a screenshot of the composite index in Firestore, and the full code for your Streambuilder
Hi @healer2030
Here is the screenshot of composite indexing and the entire Streambuilder code.

StreamBuilder(
stream: Firestore.instance
.collection('orders')
.where('userUID', isEqualTo: futureSnapshot.data.uid)
.where('orderStatus', whereIn: allOrderStatus)
.orderBy('placedAtTime')
.snapshots(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasError || snapshot == null) {
return Text('Oops! Unable to load your orders, please retry');
} else if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
semanticsLabel: 'Oops could not find any orders : )',
semanticsValue: '100%',
));
} else if (snapshot.data.documents.length == 0) {
return Center(
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
allOrderStatus.contains('Completed') ||
allOrderStatus.contains('cancelled')
? Text(
'Oops! You do not have any completed orders :)',
style: TextStyle(fontSize: 16),
textAlign: TextAlign.center,
)
: Text(
'Oops! You do not have any active orders :)',
style: TextStyle(fontSize: 16),
textAlign: TextAlign.center,
),
Container(
margin: EdgeInsets.only(top: 10.0),
child: RaisedButton(
color: Colors.green,
onPressed: () {
Navigator.pushNamedAndRemoveUntil(
context, MainScreen.id, (_) => false);
},
child: Text(
'ORDER NOW',
style: TextStyle(color: Colors.white),
),
),
)
],
),
),
);
} else {
return ListView.builder(
shrinkWrap: true,
itemCount: snapshot.data.documents.length,
itemBuilder: (context, index) {
DocumentSnapshot orderList =
snapshot.data.documents[index];
print('------orderList------');
// print(orderList['paymentStatus']);
return displayOrders(orderList['orderType'], orderList);
},
);
}
},
);
I also tried doing this for indexing:

BUT it didn't work
@swapnilshah09 is orderStatus an array?
Facing the same issue yet. Not sure if it's an issue with flutter fire or firestore itself. Below is my query.
Firestore.instance
.collection("menus")
.where("archive", isEqualTo: false)
.orderBy("createdAt", descending: true)
.snapshots()
.map((snapshot) => snapshot.documents.map((document) {
// do something here.
}).toList());
And the index looks like below

Although I don't need index on the archive field. Tried that option as well but even that fails.
Removing at least one of the clauses where or order by makes the stream query work
Thanks, @healer2030, Composite indexing worked but still, there are two issues:
* Whenever there is any change(Update in data) the stream builder is not updating. * When there is no data present then the snapshot connection state remains in waiting state. snapshot.connectionState is equal to ConnectionState.waiting
Just check your console. Flutter would tell you the error and provide a link to an already setup to be indexed fields and you merely have to click the index button. Hope it worked for you.
Facing the same issue yet. Not sure if it's an issue with flutter fire or firestore itself. Below is my query.
Firestore.instance .collection("menus") .where("archive", isEqualTo: false) .orderBy("createdAt", descending: true) .snapshots() .map((snapshot) => snapshot.documents.map((document) { // do something here. }).toList());And the index looks like below
Although I don't need index on the archive field. Tried that option as well but even that fails.
Removing at least one of the clauses
whereororder bymakes the stream query work
Try making the update on the composite tab instead of the single field.
I have the same issue. If there is data then the query returns results but if there are no results then it gets stuck on waiting.
It isn't a matter of indexing as results appear correctly as long as there the query has at least one result.
I have the same issue and no where or orderBy clauses:
stream: Firestore.instance
.document('garageOrders/${widget.orderId}')
.snapshots()
I got the solution guys:
StreamControllerstreamController.addStream(Firestore.instance.document('garageOrders/${widget.orderId}').snapshots())streamController.stream into your stream: parameter in StreamBuilderHey all, as part of our on-going work for #2582, this has been resolved in our Firebase Firestore rework (#2913) - streams now provide errors if something goes wrong like missing a query index - this has now been merged into master. We'll look at publishing some prereleases in the next few days. Thank you
Most helpful comment
Hey all, as part of our on-going work for #2582, this has been resolved in our Firebase Firestore rework (#2913) - streams now provide errors if something goes wrong like missing a query index - this has now been merged into master. We'll look at publishing some prereleases in the next few days. Thank you