Repository navigation
fix(database): generate 0-based childKeys for List values in MethodChannelDataSnapshot. - #18825
Conversation
…annelDataSnapshot
Using Gemini Code AssistThe 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
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 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. |
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 nestedMethodChannelDataSnapshotwhosevalueis aList.Why this is needed
DataSnapshot, the native Android/iOS plugin populates_data['childKeys'](for array-valued nodes,'0','1','2', ...).DataSnapshotcreated in Dart viasnapshot.child(...)(line 65) or while iteratingsnapshot.children(line 93), Dart computes_data['childKeys']locally via_childKeysFromValue(childValue):List.generatepasses a 0-basedindex(0 .. value.length - 1),'${index - 1}'produced['-1', '0', '1', ..., '${length - 2}']..childrenon that nestedListsnapshot then executes(value! as List)[int.parse(childKey)](line 83), which indexeslist[-1]on the first element and throwsRangeError (length): Invalid value: Not in inclusive range 0..2: -1.Why this does not break existing SDK behavior
_childKeysFromValueis a file-private helper inmethod_channel_data_snapshot.dartcalled only when constructing child snapshots inchild()(line 65) andchildren(line 93).Mapand scalar/nullvalues do not enter theelse if (value is List)branch.Listvalues, iterating.childrenon a nested snapshot previously threwRangeErrorunconditionally on index0(int.parse('-1')), whereassnapshot.child('0')(line 26) parses the index directly fromchildPathwithout readingchildKeys. Returning'$index'('0','1', ...) matches the 0-based keys emitted by the native Android/iOS SDKs andfirebase_database_web.Verification
387c707b2with only the new test):flutter test --no-pub test/method_channel_test.dartfails with:d61349f6e):dart format --output=none --set-exit-if-changed .(0 changes)dart analyze --fatal-infos .(0 issues)flutter test --no-pubinfirebase_database_platform_interface(74 passed, 0 failed)children returns the children for a nested list snapshotinpackages/firebase_database/firebase_database/example/integration_test/data_snapshot_e2e.dartto exercises.child('items').childrenagainst 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.yamland changelogs is not required.///).melos run analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?