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.74k stars 3.98k forks source link

[firestore]: Firestore snapshot does return empty data for existing doc #12755

Open mogol opened 7 months ago

mogol commented 7 months ago

Is there an existing issue for this?

Which plugins are affected?

Database

Which platforms are affected?

Web

Description

If the persistence is enabled for web, some snapshots return not exist even if docs do exist.

It can be reproduced when 1) firebase auth is used 2) the persistence is enabled 3) a user deletes site data

Reproducing the issue

An example project: https://github.com/mogol/firestore-web-flutter/blob/master/lib/main.dart

If persistence is off, it works as expected: it shows the latest value after tab refresh.

FirebaseFirestore.instance.settings = const Settings(persistenceEnabled: false);

Firebase Core version

2.30.1

Flutter Version

3.19.6

Relevant Log Output

No response

Flutter dependencies

Expand Flutter dependencies snippet
```yaml Dart SDK 3.3.4 Flutter SDK 3.19.6 firestore_bug 0.1.0 dependencies: - cloud_firestore 4.17.2 [cloud_firestore_platform_interface cloud_firestore_web collection firebase_core firebase_core_platform_interface flutter meta] - firebase_auth 4.19.4 [firebase_auth_platform_interface firebase_auth_web firebase_core firebase_core_platform_interface flutter meta] - firebase_core 2.30.1 [firebase_core_platform_interface firebase_core_web flutter meta] - flutter 0.0.0 [characters collection material_color_utilities meta vector_math sky_engine] dev dependencies: - flutter_lints 3.0.2 [lints] - flutter_test 0.0.0 [flutter test_api matcher path fake_async clock stack_trace vector_math leak_tracker_flutter_testing async boolean_selector characters collection leak_tracker leak_tracker_testing material_color_utilities meta source_span stream_channel string_scanner term_glyph vm_service] transitive dependencies: - _flutterfire_internals 1.3.32 [collection firebase_core firebase_core_platform_interface flutter meta] - async 2.11.0 [collection meta] - boolean_selector 2.1.1 [source_span string_scanner] - characters 1.3.0 - clock 1.1.1 - cloud_firestore_platform_interface 6.2.2 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - cloud_firestore_web 3.12.2 [_flutterfire_internals cloud_firestore_platform_interface collection firebase_core firebase_core_web flutter flutter_web_plugins] - collection 1.18.0 - fake_async 1.3.1 [clock collection] - firebase_auth_platform_interface 7.2.5 [_flutterfire_internals collection firebase_core flutter meta plugin_platform_interface] - firebase_auth_web 5.11.4 [firebase_auth_platform_interface firebase_core firebase_core_web flutter flutter_web_plugins http_parser meta web] - firebase_core_platform_interface 5.0.0 [collection flutter flutter_test meta plugin_platform_interface] - firebase_core_web 2.16.0 [firebase_core_platform_interface flutter flutter_web_plugins meta web] - flutter_web_plugins 0.0.0 [flutter characters collection material_color_utilities meta vector_math] - http_parser 4.0.2 [collection source_span string_scanner typed_data] - leak_tracker 10.0.0 [clock collection meta path vm_service] - leak_tracker_flutter_testing 2.0.1 [flutter leak_tracker leak_tracker_testing matcher meta] - leak_tracker_testing 2.0.1 [leak_tracker matcher meta] - lints 3.0.0 - matcher 0.12.16+1 [async meta stack_trace term_glyph test_api] - material_color_utilities 0.8.0 [collection] - meta 1.11.0 - path 1.9.0 - plugin_platform_interface 2.1.8 [meta] - sky_engine 0.0.99 - source_span 1.10.0 [collection path term_glyph] - stack_trace 1.11.1 [path] - stream_channel 2.1.2 [async] - string_scanner 1.2.0 [source_span] - term_glyph 1.2.1 - test_api 0.6.1 [async boolean_selector collection meta source_span stack_trace stream_channel string_scanner term_glyph] - typed_data 1.3.2 [collection] - vector_math 2.1.4 - vm_service 13.0.0 - web 0.5.1 ```

Additional context and comments

Seems related to #10331

TarekkMA commented 7 months ago

@mogol, thank you for reporting this issue and providing reproduction steps and a repo. I'll take a look.

TarekkMA commented 6 months ago

I recreated the issue, and it doesn't seem to happen when I use only JavaScript code (see the repo here). We'll look into solving this issue soon.

cc @Lyokone

fortion71 commented 5 months ago

This is more related to issue #10331 (which has been locked), but since there's a non-zero chance this might help folks that come to this issue, I'll share my specific case and the fix.

Using the Firebase Emulator Suite (may also exhibit the same behavior if using real Firebase) and a Flutter web app, I was unable to access newly created Firestore docs (which were created via a cloud function call, if that matters). Examining the the Chrome Developer Tools made the issue clear:

@firebase/firestore: Firestore (10.7.0): Document users/h1TcS3MkEFwIe6SUGPhI1gNm4EQw contains a document reference within a different database (demo-local-emu/(default)) which is not supported. It will be treated as a reference in the current database (my-real-project/(default)) instead.

So the mismatch between the local emulator and the real, remote, project name in the Firebase.initializeApp() options caused this behavior.

Hope this helps someone. Cheers

russellwheatley commented 5 months ago

Hey @mogol - I used your example main file and I couldn't reproduce 🤔. Presumably this bug is a rarity?

https://github.com/firebase/flutterfire/assets/16018629/44a5db52-6e20-4061-813a-351afb289a7e

mogol commented 5 months ago

It was 100% reproducible. I am not with my computer. But i can tell you that you don’t follow the repo steps. Try not using dev console, but settings like “normal” users Also I think my project is closed. Not sure which one you are using

russellwheatley commented 5 months ago

I just used the main.dart file. I used the steps in the issue. Settings like "normal" users deletes the same cache. I'll add the auth rule and see what happens.

mogol commented 5 months ago

I reproduced with clearing from settings and from the dev console too.

https://github.com/firebase/flutterfire/assets/3447934/fbe3d36d-ec7f-44ce-9e4e-7c6506742392 https://github.com/firebase/flutterfire/assets/3447934/67bc3b07-aaaf-448a-942b-fbf04e8e5628

I use those rules

rules_version = '2';

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}
entrenadorhispano commented 2 months ago

Hi guys, this is not always reproducible, but YES! it is a BUG.

It break some production apps in a very bad way...

Please fix it. 🙏

I had the same problem using svelte and sveltekit.

  1. Firestore DB persistence activated.
  2. User clear site data
  3. Big Mess with cache. (some docs apear as non existent)
  4. I could fix it by calling firebase/firestore -> clearIndexedDbPersistence(firestore)(https://firebase.google.com/docs/reference/js/firestore_.md#clearindexeddbpersistence_231a8e0)

But is not a full solution