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 |
Tags
- Hello World
- listview
- MainAxisAlignment
- Flutter Example
- flutter
- Image.network
- Flutter 예제
- FutureBuilder
- Flutter 강좌
- Row
- HTTP
- InkWell
- Column Widget
- sqlite
- Row Widget
- Cached Image
- Load Image
- WillPopScope
- 반석천
- AppBar
- navigator
- Scaffold
- ListView.builder
- Snackbar
- ListTile
- Networking
- Flutter Tutorial
- CrossAxisAlignment
- Flutter 앱 배포
- node.js
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 앱에서도 제공됩니다.
Flutter Code Examples - Google Play 앱
Are you a beginner at Flutter? Check out the various features of Flutter through the demo. Source code for all demos is also provided.
play.google.com
'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