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 | 31 |
Tags
- sqlite
- FutureBuilder
- Flutter 예제
- CrossAxisAlignment
- WillPopScope
- Row
- Column Widget
- HTTP
- Image.network
- Flutter Example
- ListTile
- Row Widget
- InkWell
- AppBar
- Scaffold
- Load Image
- Flutter 앱 배포
- Networking
- Flutter 강좌
- ListView.builder
- Flutter Tutorial
- 반석천
- Snackbar
- navigator
- listview
- Cached Image
- MainAxisAlignment
- node.js
- flutter
- Hello World
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