Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly call FirebaseFirestore.instance.clearPersistence() in Flutter?

I'm facing the same problem as this guy question

But his accepted answer didn't helped me.

The problem:

When an user signs out, and another different user signs in, all data shown on my app is from the previous signed out user due to firebase caching system. I searched about this issue and found a solution that consists in calling this method:

FirebaseFirestore.instance.clearPersistence();

But everytime and everywhere I place this line of code, throws an exception saying I cannot call this method when the client is running:

Exception has occurred. PlatformException (PlatformException(failed-precondition, Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console., {code: failed-precondition, message: Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console., nativeErrorMessage: Persistence cannot be cleared while the client is running., nativeErrorCode: 9}))

so, how to call this method? or better, is there a best way to solve this problem?

like image 487
Thiago Hencke Avatar asked Sep 17 '20 03:09

Thiago Hencke


People also ask

How do I retrieve data from Firebase Flutter?

To use Flutter with Firebase, you will first need to set dependencies in the pubspec file. You will also have to import firestore , i.e., the database provided by Firebase for data handling. Now, import the Firebase dependencies into your Dart file. import 'package:cloud_firestore/cloud_firestore.

What is FirebaseFirestore instance?

Gets a CollectionReference instance that refers to the collection at the specified path within the database. Query. collectionGroup(String collectionId) Creates and returns a new Query that includes all documents in the database that are contained in a collection or subcollection with the given collectionId . Task<Void ...


Video Answer


2 Answers

It seems it's necessary to terminate the FirebaseFirestore.instance first.

At the end of my log off method I call:

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

I get no errors thrown and everything seems to be working as it should now.

like image 148
Dpedrinha Avatar answered Oct 15 '22 06:10

Dpedrinha


You should call it immediately after you initialize Firebase, and before you make the first query.

like image 25
Doug Stevenson Avatar answered Oct 15 '22 06:10

Doug Stevenson