Created
November 17, 2020 08:40
-
-
Save Zfinix/2affb8a082aff70852d72181024bf356 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:convert'; | |
| import 'dart:io'; | |
| import 'package:flutter/cupertino.dart'; | |
| import 'package:flutter/material.dart'; | |
| import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | |
| import 'package:shared_preferences/shared_preferences.dart'; | |
| class LocalStorage { | |
| static Future<SharedPreferences> get prefs async => | |
| await SharedPreferences.getInstance(); | |
| static FlutterSecureStorage get securePref => FlutterSecureStorage(); | |
| static Future eraseProfileData() async { | |
| var loginData = (await getLoginData()); | |
| loginData.accessToken = null; | |
| await saveLoginData(loginModel: loginData); | |
| } | |
| static Future<LoginModel> getLoginData() async { | |
| return (await securePref.read(key: 'loginData') )!= null | |
| ? LoginModel.fromJson( | |
| json.decode(await securePref.read(key: 'loginData'))) | |
| : null; | |
| } | |
| static Future saveLoginData({@required LoginModel loginModel}) async { | |
| if (loginModel != null) | |
| return securePref.write( | |
| key: 'loginData', | |
| value: json.encode(loginModel.toJson()), | |
| ); | |
| } | |
| static Future<UserProfileModel> getProfileData() async { | |
| try { | |
| return UserProfileModel.fromJson( | |
| json.decode(await securePref.read(key: 'profileData'))); | |
| } catch (e) { | |
| return null; | |
| } | |
| } | |
| static Future saveProfileData( | |
| {@required UserProfileModel profileModel}) async { | |
| try { | |
| return securePref.write( | |
| key: 'profileData', value: json.encode(profileModel.toJson())); | |
| } catch (e) { | |
| return null; | |
| } | |
| } | |
| static Future saveItem({@required item, @required key}) async { | |
| securePref.write(key: key.toString(), value: item); | |
| } | |
| static Future eraseItem({@required key}) async { | |
| securePref.delete(key: '$key'); | |
| } | |
| static Future<bool> keyExists({@required String key}) async { | |
| if (securePref.containsKey(key: key) != null) { | |
| return true; | |
| } else { | |
| return false; | |
| } | |
| } | |
| static eraseAll() async { | |
| var savedData = await getSavedBooks(); | |
| for (var item in savedData) { | |
| try { | |
| await File(await downloadDir(item.id)).delete(); | |
| } catch (e) {} | |
| } | |
| securePref.deleteAll(); | |
| (await prefs).clear(); | |
| return; | |
| } | |
| static Future<dynamic> getItemData({@required key}) async { | |
| return await securePref.containsKey(key: '$key') | |
| ? securePref.read(key: '$key') | |
| : null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment