Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- HTTP
- Flutter 예제
- navigator
- InkWell
- sqlite
- Flutter Tutorial
- listview
- AppBar
- Hello World
- MainAxisAlignment
- flutter
- Networking
- Flutter 강좌
- ListView.builder
- FutureBuilder
- Cached Image
- Flutter Example
- node.js
- 반석천
- ListTile
- WillPopScope
- Load Image
- Image.network
- Row Widget
- Snackbar
- Column Widget
- Row
- Scaffold
- Flutter 앱 배포
- CrossAxisAlignment
Archives
- Today
- Total
꿈꾸는 시스템 디자이너
Flutter Example - Shared Preferences 본문
Add this to pubspec.yaml file
dependencies:
shared_preferences: ^0.5.3+4
Source Code
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
SharedPreferencesDemoState pageState;
class SharedPreferencesDemo extends StatefulWidget {
@override
SharedPreferencesDemoState createState() {
pageState = SharedPreferencesDemoState();
return pageState;
}
}
class SharedPreferencesDemoState extends State<SharedPreferencesDemo> {
int counter;
@override
void initState() {
super.initState();
getCounter();
}
getCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
counter = (prefs.getInt("counter") ?? 0);
});
}
setCounter() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt("counter", counter);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("Shared Preferences Demo")),
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
SizedBox(
height: 40,
child: FloatingActionButton(
heroTag: "add",
child: Icon(Icons.remove),
onPressed: () {
setState(() {
counter--;
});
setCounter();
},
),
),
Text("Shared preferences value: "),
Text(
"${counter.toString()}",
style: TextStyle(fontSize: 30),
),
SizedBox(
height: 40,
child: FloatingActionButton(
heroTag: "remove",
child: Icon(Icons.add),
onPressed: () {
setState(() {
counter++;
});
setCounter();
},
),
)
],
),
),
);
}
}
▶ Go to Table of Contents | 강의 목차로 이동
※ This example is also available in the Flutter Code Examples app. | 본 예제는 Flutter Code Examples 앱에서도 제공됩니다.
'Tutorial > Flutter with App' 카테고리의 다른 글
Flutter Example - Navigation & routing | Send data to a new screen (0) | 2019.09.27 |
---|---|
Flutter Example - Navigation & routing | Navigate to a new screen and back (2) | 2019.09.26 |
Flutter Example - SnackBar Widget (0) | 2019.09.26 |
Flutter Example - Container Widget (0) | 2019.09.26 |
Flutter Example - Expanded Widget (0) | 2019.09.24 |
Comments