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]: Firestore Termination Issue on Flutter Web: "The client has already been terminated" Error After Clearing Persistence #12831

Open sAtluna opened 4 months ago

sAtluna commented 4 months ago

Is there an existing issue for this?

Which plugins are affected?

cloud_firestore

Which platforms are affected?

Web

Description

I am using await FirebaseFirestore.instance.clearPersistence(); to clear cached data after a user signs out.

According to the documentation, this should only be called after terminating the Firestore session.

However, when I attempt to retrieve data again after logging back in, I encounter the following error:

"The client has already been terminated"

Note: This issue only occurs on Flutter Web. It works fine on Flutter mobile.

Are there any steps, which I'm missing? I already tried to recreate the whole Firebase app, but same error.

await FirebaseFirestore.instance.terminate();
await FirebaseFirestore.instance.clearPersistence();

Reproducing the issue

To reproduce the issue where clearing the cached data after a user signs out causes an error on Flutter Web, follow these steps:

After Sign Out of User: Call the following code to clear the cached data:

await FirebaseFirestore.instance.terminate();
await FirebaseFirestore.instance.clearPersistence();

Sign In User: Attempt to sign the user back in.

Retrieve Data: Try to retrieve data from Firestore. For example:

var data = await FirebaseFirestore.instance.collection('your_collection').get();

Encounter Error: Observe the following error message:

"FirebaseError: The client has already been terminated."

Note: This issue only occurs on Flutter Web. On Flutter mobile, it works as expected.

Firebase Core version

2.31.1

Flutter Version

3.19.6

Relevant Log Output

FirebaseError: The client has already been terminated.

Flutter dependencies

Expand Flutter dependencies snippet
```yaml Replace this line with the contents of your `flutter pub deps -- --style=compact`. ```

Additional context and comments

No response

bobatsar commented 4 months ago

There is also an open stackoverflow question since end of 2022 without answers :disappointed:

TarekkMA commented 4 months ago

@sAtluna Thank you for posting this issue, can you please reverse the order in which you call these methods, I found a SOF answer that suggests that.

https://stackoverflow.com/questions/70935832/app-is-crashing-in-ios-the-client-has-already-been-terminated

sAtluna commented 4 months ago

@TarekkMA, thanks for the suggestion. I tried it, but unfortunately, it didn't solve the problem. It works fine for Android and iOS, but Flutter Web is throwing the exception. Since all systems should behave the same, I'm led to think this might be a bug in Flutter Web.

TarekkMA commented 4 months ago

Yes, it appears to be coming from the JS side. The app needs to be reinitialized after terminating it using getFiresotre.

JS reproduction: https://github.com/TarekkMA/firebase-js-reproduction/tree/ff/12831