firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.61k stars 3.95k forks source link

🐛 [cloud_firestore] Cannot write to Firestore from client app when using Firestore Emulator #8178

Closed SametSahin10 closed 2 years ago

SametSahin10 commented 2 years ago

Bug report

Describe the bug I'm having problems creating documents in Firestore while using Firestore Emulator. Below is the error I see in logs:

W/Firestore(13200): (24.0.1) [WriteStream]: (aa74142) Stream closed with status: Status{code=UNKNOWN, description=null, cause=null}.

In my app, after user signs up, I check if there is a document related to this user. This read operation succeeds, therefore I conclude that I can read from Firestore. If there isn't a document related to the user, a document gets created. This is the point that the code gets stuck. The write operation does not return. It does not throw an exception either.

I'm using both Authentication and Firestore emulators.

This problem does not occur all the time. For example in the app, when a new FCM token has been generated, the token is saved into the Firestore and this works without any issues. I can see the document in Firestore Emulator Dashboard. I can also create documents manually using the dashboard.

The problem occurs both on physical device and Android Emulator. While testing on physical device, I connect to the Firestore Emulator by specifying the IP address of my computer which the Firestore Emulator is running on. On Android Emulator, I provide localhost as the host of the Firestore Emulator.

Steps to reproduce

Steps to reproduce the behavior:

  1. Set up Authentication and Firestore emulators.
  2. Try to create a document from client app.
  3. See that the write operation never returns.

Expected behavior

The document should be created without any problems.

Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 2.10.1, on Pop!_OS 21.10 5.15.23-76051523-generic, locale en_US.UTF-8) [✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0-rc1) [✓] Chrome - develop for the web [✓] Android Studio (version 2021.1) [✓] VS Code (version 1.64.2) [✓] Connected device (2 available) [✓] HTTP Host Availability • No issues found! ```

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand ``` Dart SDK 2.16.1 Flutter SDK 2.10.1 flambu 1.0.0+1 dependencies: - algolia 1.0.4 [meta http uuid] - app_tracking_transparency 2.0.2+1 [flutter] - appsflyer_sdk 6.5.2 [flutter] - auto_size_text 3.0.0 [flutter] - badges 2.0.2 [flutter] - cached_network_image 3.2.0 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web] - carousel_slider 4.0.0 [flutter] - cloud_firestore 3.1.8 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta] - confetti 0.6.0 [flutter vector_math] - country_code_picker 2.0.2 [flutter modal_bottom_sheet collection universal_platform] - dart_ipify 1.1.1 [http] - dartz 0.10.1 - file_picker 4.3.3 [flutter flutter_web_plugins flutter_plugin_android_lifecycle plugin_platform_interface ffi path] - firebase_analytics 8.3.4 [firebase_analytics_platform_interface firebase_analytics_web firebase_core flutter meta] - firebase_auth 3.3.7 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 1.12.0 [firebase_core_platform_interface firebase_core_web flutter meta] - firebase_crashlytics 2.5.0 [firebase_core firebase_core_platform_interface firebase_crashlytics_platform_interface flutter stack_trace] - firebase_messaging 11.2.6 [firebase_core firebase_core_platform_interface firebase_messaging_platform_interface firebase_messaging_web flutter meta] - firebase_storage 10.2.6 [firebase_core firebase_core_platform_interface firebase_storage_platform_interface firebase_storage_web flutter] - flutter 0.0.0 [characters collection material_color_utilities meta typed_data vector_math sky_engine] - flutter_app_badger 1.3.0 [flutter] - flutter_dotenv 5.0.2 [flutter] - flutter_fgbg 0.1.0 [flutter] - flutter_image_compress 1.1.0 [flutter] - flutter_keyboard_visibility 5.1.1 [meta flutter_keyboard_visibility_platform_interface flutter_keyboard_visibility_web flutter] - flutter_local_notifications 9.2.0 [clock flutter flutter_local_notifications_linux flutter_local_notifications_platform_interface timezone] - flutter_login_facebook 1.2.1 [flutter list_ext] - flutter_markdown 0.6.9 [flutter markdown meta path] - flutter_rating_bar 4.0.0 [flutter] - flutter_speed_dial 4.6.6 [flutter] - flutter_typeahead 3.2.4 [flutter flutter_keyboard_visibility] - form_field_validator 1.1.0 [flutter intl] - full_screen_image_null_safe 2.0.0 [flutter] - get_it 7.2.0 [async collection] - google_maps_flutter 2.1.1 [flutter flutter_plugin_android_lifecycle google_maps_flutter_platform_interface] - google_sign_in 5.2.3 [flutter google_sign_in_platform_interface google_sign_in_web] - image_downloader 0.31.0 [flutter] - infinite_scroll_pagination 3.1.0 [flutter sliver_tools] - latlong2 0.8.1 [intl] - location 4.3.0 [flutter location_platform_interface location_web] - lottie 1.2.1 [archive characters flutter path vector_math] - map_launcher 2.2.0 [flutter] - multi_image_picker 4.8.01 [flutter meta] - nil 1.1.1 [flutter] - numberpicker 2.1.1 [flutter infinite_listview] - path_provider 2.0.8 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows] - permission_handler 8.3.0 [flutter meta permission_handler_platform_interface] - pinput 1.2.2 [flutter] - provider 6.0.2 [collection flutter nested] - pull_to_refresh 2.0.0 [flutter] - readmore 2.1.0 [flutter] - reorderables 0.4.2 [flutter] - 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.12 [flutter 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.3.0 [flutter meta sign_in_with_apple_platform_interface sign_in_with_apple_web] - tab_indicator_styler 2.0.0 [flutter] - table_calendar 3.0.3 [flutter intl simple_gesture_detector] - timeline_tile 2.0.0 [flutter] - url_launcher 6.0.18 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] - uuid 3.0.5 [crypto] dev dependencies: - flutter_launcher_icons 0.9.2 [args image path yaml] - 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 material_color_utilities meta source_span stream_channel string_scanner term_glyph typed_data] transitive dependencies: - archive 3.1.11 [crypto path] - args 2.3.0 - async 2.8.2 [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] - characters 1.2.0 - charcode 1.3.1 - clock 1.1.0 - cloud_firestore_platform_interface 5.4.13 [collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 2.6.8 [cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js] - collection 1.15.0 - crypto 3.0.1 [collection typed_data] - dbus 0.6.8 [args ffi meta xml] - fake_async 1.2.0 [clock collection] - ffi 1.1.2 - 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.11 [firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 3.3.7 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.2.4 [collection flutter meta plugin_platform_interface] - firebase_core_web 1.5.4 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - firebase_crashlytics_platform_interface 3.1.13 [collection firebase_core flutter meta plugin_platform_interface] - firebase_messaging_platform_interface 3.1.6 [firebase_core flutter meta plugin_platform_interface] - firebase_messaging_web 2.2.7 [firebase_core firebase_core_web firebase_messaging_platform_interface flutter flutter_web_plugins js meta] - firebase_storage_platform_interface 4.0.13 [collection firebase_core flutter meta plugin_platform_interface] - firebase_storage_web 3.2.7 [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.3.0 [clock collection file flutter http path path_provider pedantic rxdart sqflite uuid] - flutter_keyboard_visibility_platform_interface 2.0.0 [flutter meta plugin_platform_interface] - flutter_keyboard_visibility_web 2.0.0 [flutter_keyboard_visibility_platform_interface flutter_web_plugins flutter] - flutter_local_notifications_linux 0.4.1+1 [flutter flutter_local_notifications_platform_interface dbus path xdg_directories] - flutter_local_notifications_platform_interface 5.0.0 [flutter plugin_platform_interface] - flutter_plugin_android_lifecycle 2.0.5 [flutter] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta typed_data vector_math] - google_maps_flutter_platform_interface 2.1.5 [collection flutter plugin_platform_interface stream_transform] - google_sign_in_platform_interface 2.1.1 [flutter quiver] - google_sign_in_web 0.10.0+4 [flutter flutter_web_plugins google_sign_in_platform_interface js pedantic] - http 0.13.4 [async http_parser meta path] - http_parser 4.0.0 [charcode collection source_span string_scanner typed_data] - image 3.1.1 [archive meta xml] - infinite_listview 1.1.0 [flutter] - intl 0.17.0 [clock path] - js 0.6.3 - lints 1.0.1 - list_ext 1.0.4 [quiver collection] - location_platform_interface 2.3.0 [flutter meta plugin_platform_interface] - location_web 3.1.1 [flutter flutter_web_plugins http_parser js location_platform_interface meta] - markdown 4.0.1 [args charcode meta] - matcher 0.12.11 [stack_trace] - material_color_utilities 0.1.3 - meta 1.7.0 - mime 1.0.1 - modal_bottom_sheet 2.0.0 [flutter] - nested 1.0.0 [flutter] - octo_image 1.0.1 [flutter flutter_blurhash] - path 1.8.0 - path_provider_android 2.0.11 [flutter path_provider_platform_interface] - path_provider_ios 2.0.7 [flutter path_provider_platform_interface] - path_provider_linux 2.1.5 [ffi flutter path path_provider_platform_interface xdg_directories] - path_provider_macos 2.0.5 [flutter path_provider_platform_interface] - path_provider_platform_interface 2.0.3 [flutter platform plugin_platform_interface] - path_provider_windows 2.0.5 [ffi flutter 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.1.0 - plugin_platform_interface 2.1.2 [meta] - process 4.2.4 [file path platform] - quiver 3.0.1+1 [matcher] - rxdart 0.27.3 - 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.10 [flutter shared_preferences_platform_interface] - shared_preferences_ios 2.0.9 [flutter shared_preferences_platform_interface] - shared_preferences_linux 2.0.4 [file flutter 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.3 [flutter flutter_web_plugins shared_preferences_platform_interface] - shared_preferences_windows 2.0.4 [file flutter path path_provider_platform_interface path_provider_windows shared_preferences_platform_interface] - sign_in_with_apple_platform_interface 1.0.0 [flutter plugin_platform_interface meta] - sign_in_with_apple_web 1.0.1 [flutter flutter_web_plugins sign_in_with_apple_platform_interface js] - simple_gesture_detector 0.2.0 [flutter] - sky_engine 0.0.99 - sliver_tools 0.2.5 [flutter] - source_span 1.8.1 [collection path term_glyph] - sqflite 2.0.2 [flutter sqflite_common path] - sqflite_common 2.2.0 [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.8 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher] - timezone 0.8.0 [path] - typed_data 1.3.0 [collection] - universal_platform 1.0.0+1 - url_launcher_android 6.0.14 [flutter url_launcher_platform_interface] - url_launcher_ios 6.0.14 [flutter url_launcher_platform_interface] - url_launcher_linux 2.0.3 [flutter] - url_launcher_macos 2.0.3 [flutter] - url_launcher_platform_interface 2.0.5 [flutter plugin_platform_interface] - url_launcher_web 2.0.7 [flutter flutter_web_plugins url_launcher_platform_interface] - url_launcher_windows 2.0.2 [flutter] - vector_math 2.1.1 - win32 2.3.10 [ffi] - xdg_directories 0.2.0+1 [meta path process] - xml 5.3.1 [collection meta petitparser] - yaml 3.1.0 [collection source_span string_scanner] ```

darshankawar commented 2 years ago

@SametSahin10 Thanks for the report. When we trigger a write request, it is always written to the local database first and then gets synced with the server. I think in your case, the sync may not be happening and hence the behavior you are seeing.

The error you provided Stream closed with status: Status{code=UNKNOWN, description=null, cause=null}. doesn't indicate much. Can you provide complete console or error log that may have additional relevant log that we can use to investigate further ?

google-oss-bot commented 2 years ago

Hey @SametSahin10. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

CoachJennings commented 2 years ago

Not to hijack this issue, but I'm having the same problem as @SametSahin10. When using Firebase in production, there are no read or write issues with Firestore. When using the Firebase emulators (Authentication, Firestore, and Functions), reading and writing don't appear to work as expected. Writing a document produces the same ambiguous alert:

W/Firestore(13200): (24.0.1) [WriteStream]: (aa74142) Stream closed with status: Status{code=UNKNOWN, description=null, cause=null}.

If I use an app to create a document in Firestore, I can see it appear in the Firestore Emulator Dashboard. Likewise, I can use the dashboard to create new documents manually and see them appear. However, within the app, only app-created documents appear in queries. Manually-created documents do not appear. I expect this is because, as @darshankawar wrote, the document is written to a local database before being synced with the server.

When a user logs into the app, the app requests a Firebase Function that should update a document in Firestore. Here is a slightly redacted version of the generated error from that function.

{"severity":"ERROR","message":"Error: 2 UNKNOWN: 
    at Object.callErrorFromStatus (E:\\x\\functions\\node_modules\\@grpc\\grpc-js\\build\\src\\call.js:31:26)
    at Object.onReceiveStatus (E:\\x\\functions\\node_modules\\@grpc\\grpc-js\\build\\src\\client.js:180:52)
    at Object.onReceiveStatus (E:\\x\\functions\\node_modules\\@grpc\\grpc-js\\build\\src\\client-interceptors.js:365:141)
    at Object.onReceiveStatus (E:\\x\\functions\\node_modules\\@grpc\\grpc-js\\build\\src\\client-interceptors.js:328:181)
    at E:\\x\\functions\\node_modules\\@grpc\\grpc-js\\build\\src\\call-stream.js:182:78
    at processTicksAndRejections (node:internal/process/task_queues:78:11)
Caused by: Error
    at WriteBatch.commit (E:\\x\\functions\\node_modules\\@google-cloud\\firestore\\build\\src\\write-batch.js:417:23)
    at DocumentReference.set (E:\\x\\functions\\node_modules\\@google-cloud\\firestore\\build\\src\\reference.js:355:14)
    at E:\\x\\functions\\lib\\user\\log_in.js:19:83
    at newHandler (C:\\Users\\u\\AppData\\Roaming\\nvm\\v16.14.0\\node_modules\\firebase-tools\\lib\\emulator\\functionsEmulatorRuntime.js:319:16)
    at fixedLen (E:\\x\\functions\\node_modules\\firebase-functions\\lib\\providers\\https.js:120:41)
    at E:\\x\\functions\\node_modules\\firebase-functions\\lib\\common\\providers\\https.js:400:32
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async runFunction (C:\\Users\\u\\AppData\\Roaming\\nvm\\v16.14.0\\node_modules\\firebase-tools\\lib\\emulator\\functionsEmulatorRuntime.js:564:9)
    at async runHTTPS (C:\\Users\\u\\AppData\\Roaming\\nvm\\v16.14.0\\node_modules\\firebase-tools\\lib\\emulator\\functionsEmulatorRuntime.js:590:5)
    at async handler (C:\\Users\\u\\AppData\\Roaming\\nvm\\v16.14.0\\node_modules\\firebase-tools\\lib\\emulator\\functionsEmulatorRuntime.js:510:17) {
  code: 2,
  details: '',
  metadata: Metadata {
    internalRepr: Map(1) { 'content-type' => [Array] },
    options: {}
  },
  note: 'Exception occurred in retry method that was not classified as transient'
}"}

The Firebase Function error only seems to fire once. The app error will fire repeatedly at increasing intervals. I have not tried disabling the Function and attempting to write only from the app.

SametSahin10 commented 2 years ago

The issue I was having has been resolved. I don't know which change was behind this but I remember making several upgrades in the environment that I run the Firebase Emulators. It might as well be changing the version of node or upgrading Firebase Admin SDK. You can close this issue unless you want to investigate the situation @CoachJennings has gone through.

darshankawar commented 2 years ago

Thanks for the feedback.

@CoachJennings I suggest you to open a new issues so that it can be tracked and addressed properly.

Closing per OP's latest comment.