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.66k stars 3.96k forks source link

πŸ› [cloud_firestore] Connect to Firestore and Auth emulators from real devices #8561

Closed tapizquent closed 2 years ago

tapizquent commented 2 years ago

Bug report

Describe the bug Error connecting to services when running Firebase emulators on real device.

Getting the following error when trying to make any call to Firestore or Auth

Steps to reproduce

Steps to reproduce the behavior:

  1. Create a Flutter app and import the firebase packages

  2. Configure the firebase cli and project.

  3. Start the firebase:emulator with the following firebase.json

    {
    "emulators": {
    "auth": {
      "host": "0.0.0.0",
      "port": 9099
    },
    "firestore": {
      "host": "0.0.0.0",
      "port": 8080
    },
    "ui": {
      "enabled": true,
      "port": 4000
    }
    }
    }

I also tried setting the hosts to my machine ip address

  1. Initialize firebase in the Flutter app

    FirebaseFirestore.instance.settings = Settings(
    host: 'MY-IP-ADDRESS:8080'
    sslEnabled: false,
    persistenceEnabled: false,
    );
    await FirebaseAuth.instance.useAuthEmulator(
    MY-IP-ADDRESS,
    9099,
    );
  2. Make a call to a firebase collection or to Auth

  3. See error in console and no records returned

    GTMSessionFetcher invoking fetch callbacks, data {length = 215, bytes = 0x7b0a2020 22657272 6f72223a 207b0a20 ... 205d0a20 207d0a7d }, error Error Domain=com.google.HTTPStatus Code=400 "(null)" UserInfo={data={length = 215, bytes = 0x7b0a2020 22657272 6f72223a 207b0a20 ... 205d0a20 207d0a7d }, data_content_type=application/json; charset=utf-8}
    8.14.0 - [Firebase/Firestore][I-FST000001] WatchStream (10238cad8) Stream error: 'Unknown: An internal error has occurred, print and inspect the error details for more information.'
    8.14.0 - [Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: An internal error has occurred, print and inspect the error details for more information.
    This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

I also see an error in the terminal where the simulators are running that says

Internal state invariant broken: no user with ID: XXXXXXXX
    at AgentProjectState.getUserByLocalIdAssertingExists (/usr/local/lib/node_modules/firebase-tools/lib/emulator/auth/state.js:208:19)
    at AgentProjectState.validateRefreshToken (/usr/local/lib/node_modules/firebase-tools/lib/emulator/auth/state.js:295:24)
    at grantToken (/usr/local/lib/node_modules/firebase-tools/lib/emulator/auth/operations.js:1212:38)
    at Proxy.<anonymous> (/usr/local/lib/node_modules/firebase-tools/lib/emulator/auth/server.js:375:20)
    at /usr/local/lib/node_modules/firebase-tools/node_modules/promise-breaker/index.js:265:46
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Expected behavior

Connection should be allowed in Physical Devices

Sample project


Additional context

Works great in Simulator for iOS


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` [βœ“] Flutter (Channel stable, 2.10.5, on macOS 11.3.1 20E241 darwin-x64, locale en-US) [βœ“] Android toolchain - develop for Android devices (Android SDK version 29.0.3) [βœ“] Xcode - develop for iOS and macOS (Xcode 13.2.1) [βœ“] Chrome - develop for the web [βœ“] Android Studio (version 4.1) [βœ“] VS Code (version 1.66.2) [βœ“] Connected device (3 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.2 Flutter SDK 2.10.5 vodnost 1.0.0+1 dependencies: - bs58 1.0.2 [base_x] - cached_network_image 3.2.0 [flutter flutter_cache_manager octo_image cached_network_image_platform_interface cached_network_image_web] - cloud_firestore 3.1.12 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta] - cupertino_icons 1.0.4 - enum_to_string 2.0.1 - equatable 2.0.3 [collection meta] - expandable 5.0.1 [flutter] - file_picker 4.5.1 [flutter flutter_web_plugins flutter_plugin_android_lifecycle plugin_platform_interface ffi path win32] - firebase_auth 3.3.16 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_auth_mocks 0.8.4 [flutter firebase_auth meta equatable] - firebase_core 1.14.1 [firebase_core_platform_interface firebase_core_web flutter meta] - flutter 0.0.0 [characters collection material_color_utilities meta typed_data vector_math sky_engine] - flutter_bloc 8.0.1 [flutter bloc provider] - flutter_lorem 2.0.0 [flutter] - flutter_native_splash 2.1.6 [args flutter flutter_web_plugins image js lint meta path universal_io xml yaml] - flutter_secure_storage 5.0.2 [meta flutter flutter_secure_storage_linux flutter_secure_storage_macos flutter_secure_storage_platform_interface flutter_secure_storage_web flutter_secure_storage_windows] - flutter_svg 1.0.3 [flutter meta path_drawing vector_math xml] - gap 2.0.0 [flutter] - get 4.6.1 [flutter] - global_configuration 2.0.0-nullsafety.1 [flutter http] - guid 0.0.1 [flutter equatable uuid] - image 3.1.3 [archive meta xml] - just_audio 0.9.20 [just_audio_platform_interface just_audio_web audio_session rxdart path path_provider async uuid crypto meta flutter] - marquee 2.2.1 [fading_edge_scrollview flutter] - palette_generator 0.3.3 [collection flutter path] - pinenacl 0.3.3 - quiver 3.0.1+1 [matcher] - retry 3.1.0 - routemaster 0.9.5 [flutter flutter_web_plugins path collection] - shimmer 2.0.0 [flutter] - solana 0.17.2+2 [bip39 borsh_annotation convert cryptography ed25519_hd_key freezed_annotation http json_annotation web_socket_channel] - uni_links 0.5.1 [flutter uni_links_platform_interface uni_links_web] - url_launcher 6.0.20 [flutter url_launcher_android url_launcher_ios url_launcher_linux url_launcher_macos url_launcher_platform_interface url_launcher_web url_launcher_windows] dev dependencies: - bloc_test 9.0.3 [bloc diff_match_patch meta mocktail test] - build_runner 2.1.10 [args async analyzer build build_config build_daemon build_resolvers build_runner_core code_builder collection crypto dart_style frontend_server_client glob graphs http_multi_server io js logging meta mime package_config path pool pub_semver pubspec_parse shelf shelf_web_socket stack_trace stream_transform timing watcher web_socket_channel yaml] - fake_cloud_firestore 1.2.2 [flutter cloud_firestore cloud_firestore_platform_interface collection plugin_platform_interface quiver rxdart] - 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: - _fe_analyzer_shared 31.0.0 [meta] - analyzer 2.8.0 [_fe_analyzer_shared cli_util collection convert crypto glob meta package_config path pub_semver source_span watcher yaml] - archive 3.3.0 [crypto path] - args 2.3.0 - async 2.8.2 [collection meta] - audio_session 0.1.6+1 [flutter flutter_web_plugins rxdart meta] - base_x 2.0.0 - bip39 1.0.6 [pointycastle hex crypto] - bloc 8.0.3 [meta] - boolean_selector 2.1.0 [source_span string_scanner] - borsh_annotation 0.2.0+1 - build 2.3.0 [analyzer async convert crypto glob logging meta path] - build_config 1.0.0 [checked_yaml json_annotation path pubspec_parse yaml] - build_daemon 3.1.0 [built_collection built_value http_multi_server logging path pool shelf shelf_web_socket stream_transform watcher web_socket_channel] - build_resolvers 2.0.6 [analyzer async build crypto graphs logging path package_config pool pub_semver stream_transform yaml] - build_runner_core 7.2.3 [async build build_config build_resolvers collection convert crypto glob graphs json_annotation logging meta path package_config pool timing watcher yaml] - built_collection 5.1.1 - built_value 8.2.0 [built_collection collection fixnum meta] - 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 - checked_yaml 2.0.1 [json_annotation source_span yaml] - cli_util 0.3.5 [meta path] - clock 1.1.0 - cloud_firestore_platform_interface 5.5.3 [collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 2.6.12 [cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins js] - code_builder 4.1.0 [built_collection built_value collection matcher meta] - collection 1.15.0 - convert 3.0.1 [typed_data] - coverage 1.0.3 [args logging package_config path source_maps stack_trace vm_service] - crypto 3.0.1 [collection typed_data] - cryptography 2.0.5 [collection crypto fixnum js meta typed_data] - dart_style 2.2.1 [analyzer args path pub_semver source_span] - diff_match_patch 0.4.1 - ed25519_hd_key 2.2.0 [crypto pinenacl] - fading_edge_scrollview 2.0.1 [flutter] - fake_async 1.2.0 [clock collection] - ffi 1.1.2 - file 6.1.2 [meta path] - firebase_auth_platform_interface 6.2.4 [firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 3.3.13 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser intl js meta] - firebase_core_platform_interface 4.2.5 [collection flutter meta plugin_platform_interface] - firebase_core_web 1.6.2 [firebase_core_platform_interface flutter flutter_web_plugins js meta] - fixnum 1.0.0 - flutter_blurhash 0.6.6 [flutter] - flutter_cache_manager 3.3.0 [clock collection file flutter http path path_provider pedantic rxdart sqflite uuid] - flutter_plugin_android_lifecycle 2.0.5 [flutter] - flutter_secure_storage_linux 1.1.0 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_macos 1.1.0 [flutter flutter_secure_storage_platform_interface] - flutter_secure_storage_platform_interface 1.0.0 [flutter plugin_platform_interface] - flutter_secure_storage_web 1.0.2 [flutter flutter_web_plugins flutter_secure_storage_platform_interface js] - flutter_secure_storage_windows 1.1.2 [flutter flutter_secure_storage_platform_interface] - flutter_web_plugins 0.0.0 [flutter js characters collection material_color_utilities meta typed_data vector_math] - freezed_annotation 0.14.3 [collection json_annotation meta] - frontend_server_client 2.1.2 [async path] - glob 2.0.2 [async collection file path string_scanner] - graphs 2.1.0 [collection] - hex 0.2.0 - http 0.13.4 [async http_parser meta path] - http_multi_server 3.2.0 [async] - http_parser 4.0.0 [charcode collection source_span string_scanner typed_data] - intl 0.17.0 [clock path] - io 1.0.3 [meta path string_scanner] - js 0.6.3 - json_annotation 4.4.0 [meta] - just_audio_platform_interface 4.1.0 [flutter plugin_platform_interface] - just_audio_web 0.4.7 [just_audio_platform_interface flutter flutter_web_plugins] - lint 1.8.2 - lints 1.0.1 - logging 1.0.2 - matcher 0.12.11 [stack_trace] - material_color_utilities 0.1.3 - meta 1.7.0 - mime 1.0.1 - mocktail 0.3.0 [collection matcher test] - nested 1.0.0 [flutter] - node_preamble 2.0.1 - octo_image 1.0.1 [flutter flutter_blurhash] - package_config 2.0.2 [path] - path 1.8.0 - path_drawing 1.0.0 [vector_math meta path_parsing flutter] - path_parsing 1.0.0 [vector_math meta] - path_provider 2.0.9 [flutter path_provider_android path_provider_ios path_provider_linux path_provider_macos path_provider_platform_interface path_provider_windows] - path_provider_android 2.0.12 [flutter path_provider_platform_interface] - path_provider_ios 2.0.8 [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 - petitparser 4.4.0 [meta] - platform 3.1.0 - plugin_platform_interface 2.1.2 [meta] - pointycastle 3.5.2 [collection convert js] - pool 1.5.0 [async stack_trace] - process 4.2.4 [file path platform] - provider 6.0.2 [collection flutter nested] - pub_semver 2.1.1 [collection meta] - pubspec_parse 1.2.0 [checked_yaml collection json_annotation pub_semver yaml] - rxdart 0.27.3 - shelf 1.3.0 [async collection http_parser path stack_trace stream_channel] - shelf_packages_handler 3.0.0 [path shelf shelf_static] - shelf_static 1.1.0 [convert http_parser mime path shelf] - shelf_web_socket 1.0.1 [shelf stream_channel web_socket_channel] - sky_engine 0.0.99 - source_map_stack_trace 2.1.0 [path stack_trace source_maps] - source_maps 0.10.10 [source_span] - source_span 1.8.1 [collection path term_glyph] - sqflite 2.0.2+1 [flutter sqflite_common path] - sqflite_common 2.2.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+2 - term_glyph 1.2.0 - test 1.19.5 [analyzer async boolean_selector collection coverage http_multi_server io js node_preamble package_config path pool shelf shelf_packages_handler shelf_static shelf_web_socket source_span stack_trace stream_channel typed_data web_socket_channel webkit_inspection_protocol yaml test_api test_core] - test_api 0.4.8 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph matcher] - test_core 0.4.9 [analyzer async args boolean_selector collection coverage frontend_server_client glob io meta package_config path pool source_map_stack_trace source_maps source_span stack_trace stream_channel vm_service yaml matcher test_api] - timing 1.0.0 [json_annotation] - typed_data 1.3.0 [collection] - uni_links_platform_interface 1.0.0 [flutter plugin_platform_interface] - uni_links_web 0.1.0 [flutter flutter_web_plugins uni_links_platform_interface] - universal_io 2.0.4 [collection crypto meta typed_data] - url_launcher_android 6.0.15 [flutter url_launcher_platform_interface] - url_launcher_ios 6.0.15 [flutter url_launcher_platform_interface] - url_launcher_linux 3.0.0 [flutter url_launcher_platform_interface] - url_launcher_macos 3.0.0 [flutter url_launcher_platform_interface] - url_launcher_platform_interface 2.0.5 [flutter plugin_platform_interface] - url_launcher_web 2.0.9 [flutter flutter_web_plugins url_launcher_platform_interface] - url_launcher_windows 3.0.0 [flutter url_launcher_platform_interface] - uuid 3.0.6 [crypto] - vector_math 2.1.1 - vm_service 7.5.0 - watcher 1.0.1 [async path] - web_socket_channel 2.1.0 [async crypto stream_channel] - webkit_inspection_protocol 1.0.0 [logging] - win32 2.5.1 [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

@tapizquent Thanks for the report. Are you able to replicate this issue consistently every time ?

8.14.0 - [Firebase/Firestore][I-FST000001] WatchStream (10238cad8) Stream error: 'Unknown: An internal error has occurred, print and inspect the error details for more information.' 8.14.0 - [Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: An internal error has occurred, print and inspect the error details for more information. This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

This probably indicates an error from firebase itself with combination of request not reaching the server or Dart not receiving the data from server.

host: 'MY-IP-ADDRESS:8080'

Can you try using localhost instead of using LAN IP and see if it works ?

tapizquent commented 2 years ago

Hi @darshankawar ! I tried setting to localhost instead of my IP Address and now I have.

FirebaseFirestore.instance.settings = Settings(
    host: 'localhost:8080'
    sslEnabled: false,
    persistenceEnabled: false,
);
await FirebaseAuth.instance.useAuthEmulator(
    'localhost',
    9099,
);

I'm getting a slightly different error now.

Connection 2: received failure notification
Connection 2: failed to connect 1:61, reason -1
Connection 2: encountered error(1:61)
Task <A9B25226-799D-45E8-A1E5-D59878412141>.<1> HTTP load failed, 0/0 bytes (error code: -1004 [1:61])
Task <A9B25226-799D-45E8-A1E5-D59878412141>.<1> finished with error [-1004] Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={_kCFStreamErrorCodeKey=61, NSUnderlyingError=0x283840c60 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_NSURLErrorNWPathKey=satisfied (Path is satisfied), interface: lo0, _kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <A9B25226-799D-45E8-A1E5-D59878412141>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <A9B25226-799D-45E8-A1E5-D59878412141>..<1>"
), NSLocalizedDescription=Could not connect to the server., NSErrorFailingURLStringKey=http://localhost:9099/securetoken.googleapis.com/v1/token?key=MY_API_KEY_GOES_HERE, NSErrorFailingURLKey=http://localhost:9099/securetoken.googleapis.com/v1/token?key=MY_API_KEY_GOES_HERE, _kCFStreamErrorDomainKey=1}
8.14.0 - [Firebase/Firestore][I-FST000001] WatchStream (12bd45ba8) Stream error: 'Unknown: Network error (such as timeout, interrupted connection or unreachable host) has occurred.'
8.14.0 - [Firebase/Firestore][I-FST000001] Could not reach Cloud Firestore backend. Connection failed 1 times. Most recent error: Network error (such as timeout, interrupted connection or unreachable host) has occurred.
 This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.

NOTE: Mind you that this only happens with the emulator. I have that logic behind a kDebugMode. If I run it in release or profile and it connects to the real Firebase, it works perfect.

I am testing with an iPhone XR, iOS 15.4.1.

I also tried testing it with and without WiFi and in both occasions it gives me the same error

darshankawar commented 2 years ago

Thanks for the update. Looking at the error, it seems to be network related and the request doesn't seem to be going back and forth from server to client, but I doubt if this could be related to flutterfire plugin.

I'll keep this issue open and label for further insights from the team.

/cc @russellwheatley

russellwheatley commented 2 years ago

Hey @tapizquent, to use an iOS device, you need to use your local IP address as the host. e.g:

FirebaseFirestore.instance.useFirestoreEmulator('192.XXX.X.XX', 8080);

Then in your firebase.json file for emulator config, you need to set the host to 0.0.0.0:

i.e:

"firestore": {
  "port": 8080,
  "host": "0.0.0.0"
},

Let me know if this helps!

google-oss-bot commented 2 years ago

Hey @tapizquent. 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!

HieuHongLe94 commented 2 years ago

I have a same errors.

Connection 34: received failure notification Connection 34: failed to connect 1:50, reason -1 Connection 34: encountered error(1:50)

when open app use 4G , I can't connect firebase firestore

russellwheatley commented 2 years ago

Hey @tapizquent, to use an iOS device, you need to use your local IP address as the host. e.g:

FirebaseFirestore.instance.useFirestoreEmulator('192.XXX.X.XX', 8080);

Then in your firebase.json file for emulator config, you need to set the host to 0.0.0.0:

i.e:

"firestore": {
  "port": 8080,
  "host": "0.0.0.0"
},

Let me know if this helps!

@HieuHongLe94 - did you follow this advice?

HieuHongLe94 commented 2 years ago

@russellwheatley I have erros in Both Android and IOS.

HieuHongLe94 commented 2 years ago

Please help _``` Connection 21: received failure notification Connection 21: failed to connect 1:50, reason -1 Connection 21: encountered error(1:50) Task <29D75310-CBCB-48CC-9A86-5ECAA7E84A50>.<1> HTTP load failed, 0/0 bytes (error code: -1009 [1:50]) Task <29D75310-CBCB-48CC-9A86-5ECAA7E84A50>.<1> finished with error [-1009] Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo={_kCFStreamErrorCodeKey=50, NSUnderlyingError=0x282b83270 {Error Domain=kCFErrorDomainCFNetwork Code=-1009 "(null)" UserInfo={_NSURLErrorNWPathKey=unsatisfied (Denied over cellular interface), interface: pdp_ip0, ipv4, ipv6, dns, expensive, _kCFStreamErrorCodeKey=50, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <29D75310-CBCB-48CC-9A86-5ECAA7E84A50>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask <29D75310-CBCB-48CC-9A86-5ECAA7E84A50>.<1>" ), NSLocalizedDescription=The Internet connection appears to be offline., NSErrorFailingURLStringKey=https://firebasestorage.googleapis.com:443/v0/b/dev-dadtitude.appspot.com/o?prefix=illustration/&delimiter=/, NSErrorFailingURLKey=https://firebasestorage.googleapis.com:443/v0/b/dev-dadtitude.appspot.com/o?prefix=illustration/&delimiter=/, _kCFStreamErrorDomainKey=1} 8.15.0 - [Firebase/Firestore][I-FST000001] WatchStream (1064b2b88) Stream error: 'Unavailable: DNS resolution failed for firestore.googleapis.com: UNKNOWN: nodename nor servname provided, or not known'

russellwheatley commented 2 years ago

@HieuHongLe94 are you trying to use the emulator?

HieuHongLe94 commented 2 years ago

@HieuHongLe94 are you trying to use the emulator?

No sir. I'm testing in my device and enable 4G

russellwheatley commented 2 years ago

@HieuHongLe94, This issue is about connecting to emulators. To give you a hint why you're having a problem, your console output says The Internet connection appears to be offline. You don't have an internet connection. If you have found a bug with the code, feel free to open a new issue. Thanks.