5

I'm trying to clear the Firebase Cache after a user logs out from the app. I've already tried this but the code clears all my files it seems because I had background images before logging out and all those images disappeared after logging out.

void clearCache() async{
var appDir = (await getTemporaryDirectory()).path;
new Directory(appDir).delete(recursive: true); }

This is not the effect I want. I merely just want to clear the Firebase Cache and retain the rest of my app files. How would I go about achieving this ?

Here are the logout code snippets

new FlatButton(
          child: new Text("Yes",style: new TextStyle(fontFamily: 'DroidSansChinese', color: Colors.white),),
          onPressed: () {_signOut(); clearPreferences();clearCache();},
        ),

void _signOut() async {
await _auth.signOut();
Navigator.of(context)
    .pushAndRemoveUntil(MaterialPageRoute(builder: (context) => LoginPage()), (Route<dynamic> route) => false);

}

void clearPreferences() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setString('name', '');
await prefs.setString('surname', '');
await prefs.setString('email', '');
await prefs.setString('id', '');
print(prefs.getString('name'));

}

void clearCache() async{
var appDir = (await getTemporaryDirectory()).path;
new Directory(appDir).delete(recursive: true);

}

Lebogang Nkosi
  • 327
  • 1
  • 4
  • 14
  • I believe firebase cache will disappear once you unsubscribe from the listeners. – dshukertjr Jan 25 '19 at 07:50
  • 1
    I'm not entirely sure of the behavior of Firebase cache once you do unsubscribe from the listeners. The reason why I want to implement the clearance in my app is to be completely sure that I'm getting rid of all of the previous users information. – Lebogang Nkosi Jan 25 '19 at 08:05
  • FirebaseFirestore.clearPersistence() not yet available I guess https://stackoverflow.com/questions/58902649/how-to-clear-firebase-cache – LOG_TAG Apr 01 '20 at 04:09

1 Answers1

1

but the code clears all my files

Checking the snippets you've provided, the files created by the app are deleted with Directory(appDir).delete(recursive: true);. What you have here is clearing SharedPreferences that you've stored, not necessarily clearing cache created by Firebase.

Signing out from FirebaseAuth should clear the user instance. Instead of storing user details in SharedPreferences, user details can be easily fetched while the user is logged-in with FirebaseAuth.instance.currentUser. This returns User class containing user details.

var user = FirebaseAuth.instance.currentUser;
String? name = user.displayName;
String? email = user.email;
String? id = user.uid;
Omatt
  • 8,564
  • 2
  • 42
  • 144