Skip to content

fix(database): generate 0-based childKeys for List values in MethodChannelDataSnapshot. - #18825

Open
cynthiajoan wants to merge 2 commits into
firebase:mainfrom
cynthiajoan:agent/child-keys-firebase_database_platform_interface-RTDB-4
Open

cynthiajoan wants to merge 2 commits into
firebase:mainfrom
cynthiajoan:agent/child-keys-firebase_database_platform_interface-RTDB-4

Conversation

@cynthiajoan

@cynthiajoan cynthiajoan commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes an off-by-one bug in _childKeysFromValue (packages/firebase_database/firebase_database_platform_interface/lib/src/method_channel/method_channel_data_snapshot.dart, line 107) when constructing a nested MethodChannelDataSnapshot whose value is a List.

Why this is needed

  • For a top-level DataSnapshot, the native Android/iOS plugin populates _data['childKeys'] (for array-valued nodes, '0', '1', '2', ...).
  • For a nested DataSnapshot created in Dart via snapshot.child(...) (line 65) or while iterating snapshot.children (line 93), Dart computes _data['childKeys'] locally via _childKeysFromValue(childValue):
    } else if (value is List) {
      childChildKeys = List<String>.generate(
        value.length,
        (int index) => '${index - 1}',
      );
    }
  • Because List.generate passes a 0-based index (0 .. value.length - 1), '${index - 1}' produced ['-1', '0', '1', ..., '${length - 2}'].
  • Accessing .children on that nested List snapshot then executes (value! as List)[int.parse(childKey)] (line 83), which indexes list[-1] on the first element and throws RangeError (length): Invalid value: Not in inclusive range 0..2: -1.

Why this does not break existing SDK behavior

  • _childKeysFromValue is a file-private helper in method_channel_data_snapshot.dart called only when constructing child snapshots in child() (line 65) and children (line 93).
  • Map and scalar/null values do not enter the else if (value is List) branch.
  • For non-empty List values, iterating .children on a nested snapshot previously threw RangeError unconditionally on index 0 (int.parse('-1')), whereas snapshot.child('0') (line 26) parses the index directly from childPath without reading childKeys. Returning '$index' ('0', '1', ...) matches the 0-based keys emitted by the native Android/iOS SDKs and firebase_database_web.

Verification

  • Red (before fix, on 387c707b2 with only the new test): flutter test --no-pub test/method_channel_test.dart fails with:
    RangeError (length): Invalid value: Not in inclusive range 0..2: -1 (package:firebase_database_platform_interface/src/method_channel/method_channel_data_snapshot.dart:83:40)
    
  • Green (with fix, at d61349f6e):
    • dart format --output=none --set-exit-if-changed . (0 changes)
    • dart analyze --fatal-infos . (0 issues)
    • flutter test --no-pub in firebase_database_platform_interface (74 passed, 0 failed)
    • Added integration test children returns the children for a nested list snapshot in packages/firebase_database/firebase_database/example/integration_test/data_snapshot_e2e.dart to exercise s.child('items').children against the Realtime Database emulator across all platforms.

Related Issues

N/A

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).
This will ensure a smooth and quick review process. Updating the pubspec.yaml and changelogs is not required.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

@cynthiajoan cynthiajoan changed the title fix(database): generate 0-based childKeys for List values and filter mismatched Map childKeys fix(database): generate 0-based childKeys for List values in MethodChannelDataSnapshot Oct 20, 2026
@cynthiajoan cynthiajoan changed the title fix(database): generate 0-based childKeys for List values in MethodChannelDataSnapshot fix(database): generate 0-based childKeys for List values in MethodChannelDataSnapshot [DO NOT MERGE, wait 5.1.0 release] Oct 23, 2026
@cynthiajoan cynthiajoan changed the title fix(database): generate 0-based childKeys for List values in MethodChannelDataSnapshot [DO NOT MERGE, wait 5.1.0 release] fix(database): generate 0-based childKeys for List values in MethodChannelDataSnapshot. Oct 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant