flutterfire icon indicating copy to clipboard operation
flutterfire copied to clipboard

[cloud_firestore]: Firestore Termination Issue on Flutter Web: "The client has already been terminated" Error After Clearing Persistence

Open sAtluna opened this issue 1 year ago • 5 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues.

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

Replace this line with the contents of your `flutter pub deps -- --style=compact`.

Additional context and comments

No response

sAtluna avatar May 27 '24 08:05 sAtluna

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

bobatsar avatar May 27 '24 09:05 bobatsar

@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

TarekkMA avatar May 28 '24 10:05 TarekkMA

@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.

sAtluna avatar May 29 '24 07:05 sAtluna

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

TarekkMA avatar May 29 '24 11:05 TarekkMA

Is there any update on this?

arpitmantri7501 avatar Apr 13 '25 04:04 arpitmantri7501