SharedPreferences

Clean Code Big Poo·2023년 3월 9일
0

Flutter

목록 보기
16/38
post-thumbnail

Overview

GetX를 알아보며 GetStorage에 대해서도 다루어 보았다. 그럼 Flutter.dev가 만든 SharedPreferences에 대해서도 알아봐야 하지 않겠워?
그리고 Sharedpreferences에 critical data(예를 들면 비밀번호486)은 저장하지 말라고 권고하니.. SecureSharedpreferences에 대해서도 알아보자.

둘다 adroid에서는 SharedPreferences, IOS에서는 NSUserDefaults 에 저장한다.

Sharedpreferences

flutter SharedPreferences Doc

installing 탭으로 들어가 설치하자.

Write

// Obtain shared preferences.
final prefs = await SharedPreferences.getInstance();

// Save an integer value to 'counter' key.
await prefs.setInt('counter', 10);
// Save an boolean value to 'repeat' key.
await prefs.setBool('repeat', true);
// Save an double value to 'decimal' key.
await prefs.setDouble('decimal', 1.5);
// Save an String value to 'action' key.
await prefs.setString('action', 'Start');
// Save an list of strings to 'items' key.
await prefs.setStringList('items', <String>['Earth', 'Moon', 'Sun']);

getInstance()로 SharedPreferences를 호출한다. write할때에도 await 가 필요하다.

Read

// Try reading data from the 'counter' key. If it doesn't exist, returns null.
final int? counter = prefs.getInt('counter');
// Try reading data from the 'repeat' key. If it doesn't exist, returns null.
final bool? repeat = prefs.getBool('repeat');
// Try reading data from the 'decimal' key. If it doesn't exist, returns null.
final double? decimal = prefs.getDouble('decimal');
// Try reading data from the 'action' key. If it doesn't exist, returns null.
final String? action = prefs.getString('action');
// Try reading data from the 'items' key. If it doesn't exist, returns null.
final List<String>? items = prefs.getStringList('items');

write 와 다르게 read 할때에는 Future로 받아 오지 않아 await할 필요가 없다.

활용예시 : Locale, 화면모드(다크모드, 라이트모드), DeviceInfo...

SecureSharedpreferences

flutter SecureSharedpreferences Doc

installing 탭으로 들어가 설치하자.

Sharedpreferences 와 사용법이 정말 유사하지만 isEncrypted 속성이 추가된다.
이는 암호화 할 것인지에 대한 체크이다.

Write

var pref = await SecureSharedPref.getInstance();
    await pref.putString("StringEncrypted", "This is my first string test",isEncrypted: true);
    await pref.putInt("key", 100, isEncrypted: true);
    await pref.putMap("mapKey", {"Hello":true}, isEncrypted: true);
    await pref.putDouble("doubleKey", 20.32, isEncrypted: true);
    await pref.putBool("boolKey", true,isEncrypted:  true);
    await pref.putStringList("listKey", ["S","K"], isEncrypted: true);

Read

var pref = await SecureSharedPref.getInstance();
    await pref.getString("StringEncrypted", isEncrypted: true);
    await pref.getInt("key", isEncrypted: true);
    await pref.getMap("mapKey", isEncrypted: true);
    await pref.getDouble("doubleKey", isEncrypted: true);
    await pref.getBool("boolKey",isEncrypted: true);
    await pref.getStringList("listKey", isEncrypted: true);

Sharedpreferences와 달리 read할 때에 await가 필요하다. 복호화에 필요한 시간인 걸까..

참고

https://medium.com/@mustafatahirhussein/shared-preferences-or-flutter-secure-storage-which-is-better-to-use-e6b6a0a4fcfc

0개의 댓글