Skip to content

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

Open
@russellwheatley

Description

@russellwheatley

[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
    with the firebase tag.
  • For general Firebase discussion, use the 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.

[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:
{search: 3}
{search: 4}
{search: 5}
  1. Run flutter app on an iOS device will produce the correct output:
{search: 4}
{search: 5}

The relevant android code where we call startAfter is here

Relevant Code:

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<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
 
    options: FirebaseOptions(
		// <INSERT YOUR OWN FIREBASE OPTIONS TAKEN FROM "google-services.json" FILE>		
	),
  );

  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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions