chayanforyou

shared_prefs

May 23rd, 2021 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 1.23 KB | None | 0 0
  1. import 'package:legal_legend/constants/strings.dart';
  2. import 'package:shared_preferences/shared_preferences.dart';
  3.  
  4. class SharedPrefs {
  5.  
  6.   static SharedPreferences _sharedPrefs;
  7.  
  8.   init() async {
  9.     if (_sharedPrefs == null) {
  10.       _sharedPrefs = await SharedPreferences.getInstance();
  11.     }
  12.   }
  13.  
  14.   List<int> get serviceItem => [];
  15.  
  16.   set serviceItem(List<int> serviceItem) {
  17.   }
  18.  
  19.   int get userId => _sharedPrefs.getInt(keyUserId) ?? 0;
  20.  
  21.   set userId(int value) {
  22.     _sharedPrefs.setInt(keyUserId, value);
  23.   }
  24.  
  25.   String get userName => _sharedPrefs.getString(keyUsername) ?? null;
  26.  
  27.   set userName(String value) {
  28.     _sharedPrefs.setString(keyUsername, value);
  29.   }
  30.  
  31.   String get accessToken => _sharedPrefs.getString(keyAccessToken) ?? null;
  32.  
  33.   set accessToken(String value) {
  34.     _sharedPrefs.setString(keyAccessToken, value);
  35.   }
  36.  
  37.   bool get isLoggedIn => _sharedPrefs.getBool(keyIsUserLoggedIn) ?? false;
  38.  
  39.   set isLoggedIn(bool value) {
  40.     _sharedPrefs.setBool(keyIsUserLoggedIn, value);
  41.   }
  42. }
  43.  
  44. final sharedPrefs = SharedPrefs();
  45.  
  46.  
  47. //--------------- main.dart ---------------//
  48. Future<void> main() async {
  49.   WidgetsFlutterBinding.ensureInitialized();
  50.   await sharedPrefs.init();
  51.   runApp(MyApp());
  52. }
Add Comment
Please, Sign In to add comment