firebase / firebase/firebase-android-sdk

`Query.startAfter(Double value)` returns one more document than expected.

Open
#3,597 4 comments 0 reactions 1 assignee Claimed by @jmwski View on GitHub
api: database type: bug
Dominant language
Java
Stars
2.6k
Forks
710
Avg merge
2d 9h
Merged PRs (30d)
31

Description

### [READ] Step 1: Are you in the right place?

Issues filed here should be about bugs in __the code in this repository__.
If you have a general question, need help debugging, or fall into some
other category use one of these other channels:

* For general technical questions, post a question on [StackOverflow](http://stackoverflow.com/)
with the firebase tag.
* For general Firebase discussion, use the [firebase-talk](https://groups.google.com/forum/#!forum/firebase-talk)
google group.
* For help troubleshooting your application that does not fall under one
of the above categories, reach out to the personalized
[Firebase support channel](https://firebase.google.com/support/).

### [REQUIRED] Step 2: Describe your environment

* Android Studio version: Android Studio Artic Fox 2020.3.1 Patch 4
* Firebase Component: Database
* Component version: 29.0.4

### [REQUIRED] Step 3: Describe the problem

Querying the database using the `startAfter(double value)` API is not returning the correct amount of documents (specifically returning one more than it should).

#### Steps to reproduce:
1. Run `flutter create testProject`.
2. Update "lib/main.dart" with the code under the "Relevant Code" header & input your firebase options as described in the code comment.
3. Run flutter app on an android device, the logs will output:
```bash
{search: 3}
{search: 4}
{search: 5}
```
4. Run flutter app on an iOS device will produce the correct output:
```bash
{search: 4}
{search: 5}
```

The relevant android code where we call `startAfter` is [here](https://github.com/firebase/flutterfire/blob/master/packages/firebase_database/firebase_database/android/src/main/java/io/flutter/plugins/firebase/database/QueryBuilder.java#L130)

#### Relevant Code:

```dart
import 'dart:async';

import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';

import 'firebase_options.dart';

Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(

options: FirebaseOptions(
//
),
);

runApp(
MaterialApp(
title: 'Flutter Database Example',
home: MyHomePage(),
),
);
}

class MyHomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Flutter Database Example'),
),
body: Column(
children: [
ElevatedButton(
onPressed: () async {
//CREATES 5 DOCUMENTS IN DATABASE
DatabaseReference _testsRef = FirebaseDatabase.instance.ref('tests');
await _testsRef.set({
'yo':{'search':2,},
'so':{'search':3,},
'to':{'search':4,},
'fu':{'search':1,},
'bo':{'search':5,},
});
// QUERY THAT SHOULD RETURN 2 DOCUMENTS IF STARTING AFTER 3 (THE THIRD DOCUMENT)
DataSnapshot response = await _testsRef.orderByChild('search').startAfter(3).get();

response.children.forEach((element) {
// PRINTS OUT THE DOCUMENTS RETURNED
print(element.value.toString());
});
},
child: const Text('Press me'),
),
],
),
);
}
}
```

Hopefully, this makes sense as a flutter implementation! It is very straight forward. In a nutshell;
1. Create 5 documents.
2. Use orderByChild(String path) & startAfter(Double value) to query documents, and note that the query returns one more document than expected.

Let me know if you need any further information.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.