Closed Tom3652 closed 2 years ago
Hi @Tom3652, Thanks for filing the issue. Could you please share the version of the database plugin and the structure of your database?
Thanks
Hello @maheshmnj thanks for your reply.
I am using : firebase_database: ^8.1.0
Here is a screenshot of my structure :
But as i wrote in my sample code, this doesn't seem to be related to my database structure => having a simple node with messages
only can reproduce the issue, there is no limit nor order by :/
Please let me know if you need more specifications / details.
Thanks for the info @Tom3652. I can reproduce the issue limitToFirst
and limitToLast
methods are not limiting the number of results to be returned it always returns the whole data.
firebase_core: ^1.10.0
firebase_database: ^8.1.0
Thanks @maheshmnj for confirming :)
Should i open another issue regarding the orderByChild
as well ?
I am aware that there is an ongoing rework of this plugin and that it's still written in beta
in the FlutterFire doc, so the team is probably aware of all these bugs as they are really "simple" use cases.
@Tom3652 sure please.
Alright my bad, it's the get
method that returns a complete desordered results with orderByChild
, but the once
is working fine along with orderByChild
.
Since i don't know if this is a normal behavior for the get
method, i will not make an issue for now :)
I simply would like to specify that the issue only happens when using the .get()
method.
Fortunately i can use the .once()
so i am not stuck anymore :)
Are you able to try the latest plugin version and confirm if this is still an issue.
I have a feeling there's an underlying issue with this though on the official Firebase SDKs that we wrap - but would be good to confirm how the latest FlutterFire plugin version behaves - thanks
I also confirm that it's working fine with the version : firebase_database: ^9.0.3
.
However, i had to add an index to make it work, otherwise i got the below error :
I/RepoOperation( 3761): get for query /messages falling back to disk cache after error: Index not defined, add ".indexOn": "createdAt", for path "/messages", to the rules
E/firebase_database( 3761): An unknown error occurred handling native method call Query#get
E/firebase_database( 3761): java.util.concurrent.ExecutionException: java.lang.Exception: Index not defined, add ".indexOn": "createdAt", for path "/messages", to the rules
E/firebase_database( 3761): at com.google.android.gms.tasks.Tasks.zza(com.google.android.gms:play-services-tasks@@17.2.1:5)
E/firebase_database( 3761): at com.google.android.gms.tasks.Tasks.await(com.google.android.gms:play-services-tasks@@17.2.1:8)
E/firebase_database( 3761): at io.flutter.plugins.firebase.database.FirebaseDatabasePlugin.lambda$queryGet$8$FirebaseDatabasePlugin(FirebaseDatabasePlugin.java:248)
E/firebase_database( 3761): at io.flutter.plugins.firebase.database.FirebaseDatabasePlugin$$ExternalSyntheticLambda17.call(Unknown Source:4)
E/firebase_database( 3761): at com.google.android.gms.tasks.zzx.run(com.google.android.gms:play-services-tasks@@17.2.1:1)
E/firebase_database( 3761): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/firebase_database( 3761): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/firebase_database( 3761): at java.lang.Thread.run(Thread.java:919)
E/firebase_database( 3761): Caused by: java.lang.Exception: Index not defined, add ".indexOn": "createdAt", for path "/messages", to the rules
E/firebase_database( 3761): at com.google.firebase.database.connection.PersistentConnectionImpl.lambda$get$0$PersistentConnectionImpl(PersistentConnectionImpl.java:426)
E/firebase_database( 3761): at com.google.firebase.database.connection.PersistentConnectionImpl$$ExternalSyntheticLambda2.onResponse(Unknown Source:6)
E/firebase_database( 3761): at com.google.firebase.database.connection.PersistentConnectionImpl$6.onResponse(PersistentConnectionImpl.java:1315)
E/firebase_database( 3761): at com.google.firebase.database.connection.PersistentConnectionImpl.onDataMessage(PersistentConnectionImpl.java:513)
E/firebase_database( 3761): at com.google.firebase.database.connection.Connection.onDataMessage(Connection.java:167)
E/firebase_database( 3761): at com.google.firebase.database.connection.Connection.onMessage(Connection.java:131)
E/firebase_database( 3761): at com.google.firebase.database.connection.WebsocketConnection.appendFrame(WebsocketConnection.java:259)
E/firebase_database( 3761): at com.google.firebase.database.connection.WebsocketConnection.handleIncomingFrame(WebsocketConnection.java:306)
E/firebase_database( 3761): at com.google.firebase.database.connection.WebsocketConnection.access$500(WebsocketConnection.java:34)
E/firebase_database( 3761): at com.google.firebase.database.connection.WebsocketConnection$WSClientTubesock$2.run(WebsocketConnection.java:86)
E/firebase_database( 3761): at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
E/firebase_database( 3761): at java.util.concurrent.FutureTask.run(FutureTask.java:266)
E/firebase_database( 3761): at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
E/firebase_database( 3761): ... 3 more
E/flutter ( 3761): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [firebase_database/index-not-defined] Index not defined, add ".indexOn": "createdAt", for path "/messages", to the rules
E/flutter ( 3761):
E/flutter ( 3761): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
E/flutter ( 3761): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:167:18)
E/flutter ( 3761): <asynchronous suspension>
E/flutter ( 3761): #2 MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:367:43)
E/flutter ( 3761): <asynchronous suspension>
E/flutter ( 3761): #3 MethodChannelQuery.get (package:firebase_database_platform_interface/src/method_channel/method_channel_query.dart:74:22)
E/flutter ( 3761): <asynchronous suspension>
E/flutter ( 3761): #4 Query.get (package:firebase_database/src/query.dart:21:27)
E/flutter ( 3761): <asynchronous suspension>
E/flutter ( 3761): #5 _TestAppState._sortMessagesDescendingWithLimit (package:testapp/test_fire.dart:37:33)
E/flutter ( 3761): <asynchronous suspension>
E/flutter ( 3761):
E/flutter ( 3761): #0 MethodChannelQuery.get (package:firebase_database_platform_interface/src/method_channel/method_channel_query.dart:86:7)
E/flutter ( 3761): <asynchronous suspension>
E/flutter ( 3761): #1 Query.get (package:firebase_database/src/query.dart:21:27)
E/flutter ( 3761): <asynchronous suspension>
E/flutter ( 3761): #2 _TestAppState._sortMessagesDescendingWithLimit (package:testapp/test_fire.dart:37:33)
E/flutter ( 3761): <asynchronous suspension>
After adding the index as required it gives the wanted output.
great thanks for confirming this is now working
Bug report
I am trying to perform a simple query :
And i expect to retrieve only 1 result from the end of the list (ascending order). I have 5 messages under the
messages
node here, and the query returns them all instead of the more recent one only.Same issue using
limitToFirst(1)
that retrieves all data under themessages
node.I have also tried using
orderByChild
which doesn't change anything.Steps to reproduce
Steps to reproduce the behavior:
Expected behavior
ascending
and limited to 1 result when using :=> Output is unordered and definitely not with 1 item.
=> Same.
With the sample code, the expected output would be
message4
as content only.Additional issue
The
orderByChild
method is also not working. I have set negative timestamps to thecreatedAt
to be able to order bydescending
.However, the
orderByChild
is not working, we can see (despite thelimitToLast(1)
) in the sample example the whole list of messages still unordered.Sample project
Flutter doctor
Click To Expand
``` Doctor summary (to see all details, run flutter doctor -v): [β] Flutter (Channel stable, 2.5.3, on macOS 12.0.1 21A559 darwin-x64, locale fr-FR) [β] Android toolchain - develop for Android devices (Android SDK version 31.0.0) [β] Xcode - develop for iOS and macOS [β] Chrome - develop for the web [β] Android Studio (version 2020.3) [β] VS Code (version 1.62.3) [β] Connected device (3 available) β’ No issues found! ```Flutter dependencies
Run
flutter pub deps -- --style=compact
and paste the output below:Click To Expand
``` Dart SDK 2.14.4 Flutter SDK 2.5.3 myapp 1.0.0+1 dependencies: - app_settings 4.1.1 [flutter] - app_tracking_transparency 2.0.2+1 [flutter] - apple_maps_flutter 1.0.1 [flutter] - badges 2.0.1 [flutter] - cached_network_image 3.1.0+1 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web] - camera 0.9.4+5 [camera_platform_interface camera_web flutter pedantic quiver flutter_plugin_android_lifecycle] - clipboard 0.1.3 [flutter] - cloud_functions 3.1.1 [cloud_functions_platform_interface cloud_functions_web firebase_core firebase_core_platform_interface flutter] - connectivity_plus 2.0.3 [flutter connectivity_plus_platform_interface connectivity_plus_linux connectivity_plus_macos connectivity_plus_web connectivity_plus_windows] - country_code_picker 2.0.2 [flutter modal_bottom_sheet collection universal_platform] - crypto 3.0.1 [collection typed_data] - cupertino_back_gesture 0.1.0 [flutter] - cupertino_icons 1.0.4 - disk_space 0.2.1 [flutter] - emoji_picker_flutter 1.0.8 [flutter shared_preferences] - extended_image 5.1.3 [extended_image_library flutter meta] - extended_tabs 2.3.0 [flutter] - ffmpeg_kit_flutter 4.5.0 [ffmpeg_kit_flutter_platform_interface flutter] - firebase_analytics 8.3.4 [firebase_analytics_platform_interface firebase_analytics_web firebase_core flutter meta] - firebase_auth 3.2.0 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 1.10.0 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_crashlytics 2.3.0 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - firebase_database 8.1.0 [firebase_core firebase_database_platform_interface firebase_database_web flutter] - firebase_dynamic_links 3.0.1 [firebase_core flutter] - firebase_messaging 11.1.0 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - firebase_performance 0.7.1+4 [firebase_core firebase_core_platform_interface firebase_performance_platform_interface firebase_performance_web flutter] - firebase_storage 10.1.0 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter] - flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine] - flutter_app_badger 1.3.0 [flutter] - flutter_arc_text 0.5.0 [characters flutter] - flutter_contacts 1.1.1+2 [flutter] - flutter_facebook_auth 3.5.6 [flutter flutter_facebook_auth_platform_interface flutter_facebook_auth_web] - flutter_image_compress 1.1.0 [flutter] - flutter_launcher_icons 0.9.2 [args image path yaml] - flutter_localizations 0.0.0 [flutter intl characters clock collection meta path typed_data vector_math] - flutter_sound_lite 8.4.2 [path_provider recase uuid provider path synchronized logger flutter flutter_sound_platform_interface flutter_sound_web] - flutter_staggered_grid_view 0.4.1 [flutter] - flutter_svg 0.23.0+1 [flutter meta path_drawing vector_math xml] - geocoding 2.0.1 [flutter geocoding_platform_interface] - geolocator 7.7.1 [flutter geolocator_platform_interface geolocator_android geolocator_apple geolocator_web] - google_fonts 2.1.0 [flutter http path_provider crypto] - google_maps_flutter 2.1.1 [flutter flutter_plugin_android_lifecycle google_maps_flutter_platform_interface] - google_mobile_ads 1.0.0 [meta flutter] - google_sign_in 5.2.1 [flutter google_sign_in_platform_interface google_sign_in_web meta] - hive 2.0.4 [meta crypto] - hive_flutter 1.1.0 [flutter hive path_provider path] - http 0.13.4 [async http_parser meta path] - image_editor 1.0.2 [flutter] - in_app_review 2.0.3 [flutter in_app_review_platform_interface] - intl 0.17.0 [clock path] - logger 1.1.0 - native_admob_flutter 1.5.0 [flutter] - path 1.8.0 - path_provider 2.0.7 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows] - percent_indicator 3.4.0 [flutter] - perfect_volume_control 1.0.5 [flutter] - permission_handler 8.3.0 [flutter meta permission_handler_platform_interface] - photo_manager 1.3.10 [flutter] - provider 6.0.1 [collection flutter nested] - pull_to_refresh 2.0.0 [flutter] - rive 0.7.33 [collection flutter graphs http meta] - scrollable_positioned_list 0.2.3 [flutter collection] - share_plus 3.0.4 [meta mime flutter share_plus_platform_interface share_plus_linux share_plus_macos share_plus_windows share_plus_web] - shared_preferences 2.0.9 [flutter meta shared_preferences_android shared_preferences_ios shared_preferences_linux shared_preferences_macos shared_preferences_platform_interface shared_preferences_web shared_preferences_windows] - sign_in_with_apple 3.2.0 [flutter meta] - sliver_tools 0.2.5 [flutter] - sqflite 2.0.0+4 [flutter sqflite_common path] - translator 0.1.7 [http] - url_launcher 6.0.14 [flutter meta url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - uuid 3.0.5 [crypto] - video_compress 3.1.1 [flutter] - video_player 2.2.7 [flutter meta video_player_platform_interface video_player_web html] - video_thumbnail 0.4.3 [flutter] - visibility_detector 0.2.2 [flutter] dev dependencies: - flutter_lints 1.0.4 [lints] - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data] transitive dependencies: - archive 3.1.6 [crypto path] - args 2.3.0 - async 2.8.1 [collection meta] - boolean_selector 2.1.0 [source_span string_scanner] - cached_network_image_platform_interface 1.0.0 [flutter flutter_cache_manager] - cached_network_image_web 1.0.1 [flutter flutter_cache_manager cached_network_image_platform_interface] - camera_platform_interface 2.1.1 [cross_file flutter meta plugin_platform_interface stream_transform] - camera_web 0.2.1+1 [camera_platform_interface flutter flutter_web_plugins stream_transform] - characters 1.1.0 - charcode 1.3.1 - clock 1.1.0 - cloud_functions_platform_interface 5.0.14 [firebase_core flutter meta plugin_platform_interface] - cloud_functions_web 4.1.1 [cloud_functions_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins js] - collection 1.15.0 - connectivity_plus_linux 1.1.1 [flutter connectivity_plus_platform_interface meta nm] - connectivity_plus_macos 1.2.1 [connectivity_plus_platform_interface flutter] - connectivity_plus_platform_interface 1.1.1 [flutter meta plugin_platform_interface] - connectivity_plus_web 1.1.0+1 [connectivity_plus_platform_interface flutter_web_plugins flutter] - connectivity_plus_windows 1.2.0 [connectivity_plus_platform_interface flutter] - cross_file 0.3.2 [flutter js meta] - csslib 0.17.1 [source_span] - dbus 0.6.6 [args ffi meta xml] - extended_image_library 3.1.0 [crypto flutter http_client_helper path path_provider] - fake_async 1.2.0 [clock collection] - ffi 1.1.2 - ffmpeg_kit_flutter_platform_interface 0.1.0 [flutter plugin_platform_interface] - file 6.1.2 [meta path] - firebase 9.0.2 [http http_parser js] - firebase_analytics_platform_interface 2.0.1 [flutter meta] - firebase_analytics_web 0.3.0+1 [firebase firebase_analytics_platform_interface flutter flutter_web_plugins meta] - firebase_auth_platform_interface 6.1.4 [firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 3.2.0 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.1.0 [collection flutter meta plugin_platform_interface] - firebase_core_web 1.2.0 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_crashlytics_platform_interface 3.1.6 [collection firebase_core flutter meta plugin_platform_interface] - firebase_database_platform_interface 0.1.0+3 [collection firebase_core flutter meta plugin_platform_interface] - firebase_database_web 0.1.1 [firebase_core firebase_core_web firebase_database_platform_interface flutter flutter_web_plugins js] - firebase_messaging_platform_interface 3.0.9 [firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 2.1.0 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] - firebase_performance_platform_interface 0.0.1+7 [firebase_core flutter plugin_platform_interface] - firebase_performance_web 0.0.2 [firebase firebase_performance_platform_interface flutter flutter_web_plugins] - firebase_storage_platform_interface 4.0.6 [collection firebase_core flutter meta plugin_platform_interface] - firebase_storage_web 3.1.0 [async firebase_core firebase_core_web firebase_storage_platform_interface flutter flutter_web_plugins http js meta] - flutter_blurhash 0.6.0 [flutter meta pedantic] - flutter_cache_manager 3.1.3 [clock collection file flutter http path path_provider pedantic rxdart sqflite uuid] - flutter_facebook_auth_platform_interface 2.7.0 [flutter plugin_platform_interface] - flutter_facebook_auth_web 2.6.0+2 [flutter flutter_web_plugins js flutter_facebook_auth_platform_interface] - flutter_plugin_android_lifecycle 2.0.4 [flutter] - flutter_sound_platform_interface 8.4.2 [flutter meta plugin_platform_interface logger] - flutter_sound_web 8.4.2 [flutter_sound_platform_interface flutter logger flutter_web_plugins meta js] - flutter_web_plugins 0.0.0 [flutter js characters collection meta typed_data vector_math] - geocoding_platform_interface 2.0.1 [flutter meta plugin_platform_interface] - geolocator_android 2.1.0 [flutter geolocator_platform_interface] - geolocator_apple 1.2.2 [flutter geolocator_platform_interface] - geolocator_platform_interface 2.3.6 [flutter plugin_platform_interface vector_math meta] - geolocator_web 2.0.6 [flutter flutter_web_plugins geolocator_platform_interface] - google_maps_flutter_platform_interface 2.1.3 [collection flutter meta plugin_platform_interface stream_transform] - google_sign_in_platform_interface 2.1.0 [flutter meta quiver] - google_sign_in_web 0.10.0+3 [flutter flutter_web_plugins google_sign_in_platform_interface js meta] - graphs 2.1.0 [collection] - html 0.15.0 [csslib source_span] - http_client_helper 2.0.2 [http] - http_parser 4.0.0 [charcode collection source_span string_scanner typed_data] - image 3.0.8 [archive meta xml] - in_app_review_platform_interface 2.0.2 [flutter url_launcher plugin_platform_interface platform] - js 0.6.3 - lints 1.0.1 - matcher 0.12.10 [stack_trace] - meta 1.7.0 - mime 1.0.1 - modal_bottom_sheet 2.0.0 [flutter] - nested 1.0.0 [flutter] - nm 0.4.1 [dbus] - octo_image 1.0.0+1 [flutter flutter_blurhash] - path_drawing 0.5.1+1 [vector_math meta path_parsing flutter] - path_parsing 0.2.1 [vector_math meta] - path_provider_android 2.0.8 [flutter path_provider_platform_interface] - path_provider_ios 2.0.7 [flutter path_provider_platform_interface] - path_provider_linux 2.1.2 [flutter path path_provider_platform_interface xdg_directories] - path_provider_macos 2.0.3 [flutter] - path_provider_platform_interface 2.0.1 [flutter meta platform plugin_platform_interface] - path_provider_windows 2.0.4 [ffi flutter meta path path_provider_platform_interface win32] - pedantic 1.11.1 - permission_handler_platform_interface 3.7.0 [flutter meta plugin_platform_interface] - petitparser 4.4.0 [meta] - platform 3.0.2 - plugin_platform_interface 2.0.2 [meta] - process 4.2.4 [file path platform] - quiver 3.0.1+1 [matcher] - recase 4.0.0 - rxdart 0.27.2 - share_plus_linux 2.0.4 [share_plus_platform_interface file flutter meta url_launcher] - share_plus_macos 2.0.2 [share_plus_platform_interface flutter] - share_plus_platform_interface 2.0.1 [flutter meta mime plugin_platform_interface] - share_plus_web 2.0.4 [share_plus_platform_interface url_launcher flutter flutter_web_plugins meta] - share_plus_windows 2.0.3 [share_plus_platform_interface flutter meta url_launcher] - shared_preferences_android 2.0.8 [flutter meta shared_preferences_platform_interface] - shared_preferences_ios 2.0.8 [flutter meta shared_preferences_platform_interface] - shared_preferences_linux 2.0.3 [file flutter meta path path_provider_linux shared_preferences_platform_interface] - shared_preferences_macos 2.0.2 [flutter shared_preferences_platform_interface] - shared_preferences_platform_interface 2.0.0 [flutter] - shared_preferences_web 2.0.2 [flutter flutter_web_plugins meta shared_preferences_platform_interface] - shared_preferences_windows 2.0.3 [file flutter meta path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface] - sky_engine 0.0.99 - source_span 1.8.1 [collection path term_glyph] - sqflite_common 2.0.1+1 [synchronized path meta] - stack_trace 1.10.0 [path] - stream_channel 2.1.0 [async] - stream_transform 2.0.0 - string_scanner 1.1.0 [charcode source_span] - synchronized 3.0.0 - term_glyph 1.2.0 - test_api 0.4.2 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher] - typed_data 1.3.0 [collection] - universal_platform 1.0.0+1 - url_launcher_linux 2.0.2 [flutter] - url_launcher_macos 2.0.2 [flutter] - url_launcher_platform_interface 2.0.4 [flutter plugin_platform_interface] - url_launcher_web 2.0.4 [flutter flutter_web_plugins meta url_launcher_platform_interface] - url_launcher_windows 2.0.2 [flutter] - vector_math 2.1.0 - video_player_platform_interface 4.2.0 [flutter flutter_test meta] - video_player_web 2.0.4 [flutter flutter_web_plugins meta video_player_platform_interface] - win32 2.3.0 [ffi] - xdg_directories 0.2.0 [meta path process] - xml 5.3.1 [collection meta petitparser] - yaml 3.1.0 [collection source_span string_scanner] ```