Search

SharedPreferences

SharedPreferences

SharedPreferences๋Š” Flutter์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋กœ์ปฌ ๋ฐ์ดํ„ฐ ์ €์žฅ ํด๋ž˜์Šค๋กœ, ์•ฑ์˜ ์˜๊ตฌ์ ์ธ ์ƒํƒœ๋ฅผ ์ €์žฅํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ ์•ฑ ์„ค์ •, ์‚ฌ์šฉ์ž ํ”„๋กœํ•„ ์ •๋ณด, ํ† ํฐ ๋“ฑ์„ ์ €์žฅํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.
โ€ข
Android : XML ํŒŒ์ผ ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅ
โ€ข
iOS : plist ํŒŒ์ผ๋กœ ์ €์žฅ
๋ฒ”์œ„(Scope): ์•ฑ ๋‚ด์—์„œ ๊ณต์œ ๋˜๋Š” ํ‚ค-๊ฐ’ ์Œ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

shared_preferences ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜ํ•˜๊ธฐ

dependencies (์˜์กด์„ฑ) ์— ์ถ”๊ฐ€ํ•˜๊ธฐ

pubspec.yaml

shared_preferences: ^2.2.2
YAML
๋ณต์‚ฌ

Flutter ๋ช…๋ น์–ด๋กœ ์„ค์น˜ํ•˜๊ธฐ

flutter pub add shared_preferences
Bash
๋ณต์‚ฌ

shared_preferences import ํ•˜๊ธฐ

shared_preferences
Dart
๋ณต์‚ฌ

shared_preferences ์ฃผ์š” ๋ฉ”์†Œ๋“œ

๋ฉ”์†Œ๋“œ
์„ค๋ช…
getInstance()
SharedPreferencesย ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
setString()
key์—ย value๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
getString()
key์— ์ €์žฅ๋œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
setInt()
key์—ย value๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
getInt()
key์— ์ €์žฅ๋œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
setDouble()
key์—ย value๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
getDouble()
key์— ์ €์žฅ๋œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
setBool()
key์—ย value๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
getBool()
key์— ์ €์žฅ๋œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
remove()
key์— ์ €์žฅ๋œ ๊ฐ’์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.
clear()
๋ชจ๋“  ๊ฐ’์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

์ธ์Šคํ„ด์Šค ๊ฐ€์ ธ์˜ค๊ธฐ

// SharedPreferences ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. SharedPreferences prefs = await SharedPreferences.getInstance();
Dart
๋ณต์‚ฌ

setter ๋ฉ”์†Œ๋“œ๋กœ ๊ฐ’ ์ €์žฅํ•˜๊ธฐ

// `key`์— `value`๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค. prefs.setString("key", "value");
Dart
๋ณต์‚ฌ

getter ๋ฉ”์†Œ๋“œ๋กœ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

// `key`์— ์ €์žฅ๋œ ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. String value = prefs.getString("key");
Dart
๋ณต์‚ฌ

shared_preferences ์นด์šดํŠธ ์•ฑ ์นด์šดํŠธ ์ €์žฅํ•˜๊ธฐ

main.dart

import 'package:flutter/material.dart'; import 'package:shared_data_app/file_app.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { // SharedPrefences ๊ณต์œ  ํ™˜๊ฒฝ์„ค์ •์— ๋ฐ์ดํ„ฐ ์ €์žฅ ์˜ˆ์ œ runApp(const MyApp()); // } class MyApp extends StatelessWidget { const MyApp({super.key}); Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), home: const MyHomePage(title: 'Flutter Demo Home Page'), ); } } class MyHomePage extends StatefulWidget { const MyHomePage({super.key, required this.title}); final String title; State<MyHomePage> createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { int _counter = 0; void initState() { super.initState(); // ๊ณต์œ  ํ™˜๊ฒฝ์„ค์ •์— ์ €์žฅํ•ด๋†“์€ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ _getData(); } void _incrementCounter() { setState(() { _counter++; // + ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ, ๋ณ€๊ฒฝ๋œ counter ๊ฐ’ ์ €์žฅ _setData(_counter); }); } // SharedPrefeces ๋ฐ์ดํ„ฐ ์ €์žฅํ•˜๋Š” ํ•จ์ˆ˜ void _setData(int value) async { var key = 'count'; SharedPreferences pref = await SharedPreferences.getInstance(); pref.setInt(key, value); } // SharedPrefeces ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๋Š” ํ•จ์ˆ˜ void _getData() async { var key = 'count'; SharedPreferences pref = await SharedPreferences.getInstance(); setState(() { var value = pref.getInt(key); _counter = value ?? 0; }); } Widget build(BuildContext context) { return Scaffold( appBar: AppBar( backgroundColor: Theme.of(context).colorScheme.inversePrimary, title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ const Text( 'You have pushed the button this many times:', ), Text( '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Increment', child: const Icon(Icons.add), ), ); } }
Dart
๋ณต์‚ฌ

pubspec.yaml

name: shared_data_app description: A new Flutter project. publish_to: none version: 1.0.0+1 environment: sdk: '>=3.2.3 <4.0.0' dependencies: flutter: sdk: flutter shared_preferences: ^2.2.2 cupertino_icons: ^1.0.2 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^2.0.0 flutter: uses-material-design: true # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg
YAML
๋ณต์‚ฌ
์นด์šดํŠธ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค๊ณ  ์•ฑ์„ ์ข…๋ฃŒ์‹œ์ผฐ๋‹ค๊ฐ€ ๋‹ค์‹œ ์ผœ๋„, ๊ทธ๋Œ€๋กœ 5๋กœ ๋‚จ์•„์žˆ๋‹ค.