简体   繁体   中英

Flutter : How to clear Firebase cache after logging out from app?

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);

}

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM