Search
๐Ÿน

Flutter Local Storage (iOS: UserDefaults, AOS: SharedPreference)

์ƒ์„ฑ์ผ
2021/06/06 07:44
ํƒœ๊ทธ
Flutter
์†์„ฑ

Shared Preferences

Dependency

flutter pub add shared_preferences
Dart
๋ณต์‚ฌ

Example

final pref = await SharedPreferences.getInstance(); bool isFirstLaunched = pref.getBool('isFirstLaunched') ?? false;
Dart
๋ณต์‚ฌ
SharedPreferences์—์„œ getInstance() ๋กœ local storage ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. getInstance ์˜ return value ๋Š” Future ๋กœ ๋น„๋™๊ธฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์•„๋งˆ iOS ๊ธฐ์ค€์œผ๋กœ UserDefault๋‚˜ keychain ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์•ฑ ์‹คํ–‰ ์ดํ›„ ์‚ฌ์šฉ ์ค€๋น„๊ฐ€ ์™„๋ฃŒ๋˜๊ธฐ๊นŒ์ง€ ๋Œ€๊ธฐ ์‹œ๊ฐ„์ด ํ•„์š”ํ•œ๋ฐ ๊ทธ ์‹œ๊ฐ„์„ ์œ„ํ•จ์ด ์•„๋‹Œ๊ฐ€ ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
SharedPreferences๋Š” ์œ„์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฐ˜ํ™˜๊ฐ’์€ nullable ํ•˜๋ฏ€๋กœ, ?? ๋ฅผ ์ด์šฉํ•ด์„œ ํ•ด๋‹น key ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.