「Flutter/外部ライブラリ/shared preferences」の版間の差分
提供: 初心者エンジニアの簡易メモ
行9: | 行9: | ||
flutter packages get | flutter packages get | ||
+ | |||
+ | ==サンプル== | ||
+ | import 'package:shared_preferences/shared_preferences.dart'; | ||
+ | <pre> | ||
+ | _incrementCounter() async { | ||
+ | SharedPreferences prefs = await SharedPreferences.getInstance(); | ||
+ | int counter = (prefs.getInt('counter') ?? 0) + 1; | ||
+ | print('Pressed $counter times.'); | ||
+ | await prefs.setInt('counter', counter); | ||
+ | } | ||
+ | </pre> | ||
==参考== | ==参考== |
2019年4月26日 (金) 10:37時点における版
pubspec.yaml
dependencies: flutter: sdk: flutter shared_preferences: ^0.5.2
flutter packages get
サンプル
import 'package:shared_preferences/shared_preferences.dart';
_incrementCounter() async { SharedPreferences prefs = await SharedPreferences.getInstance(); int counter = (prefs.getInt('counter') ?? 0) + 1; print('Pressed $counter times.'); await prefs.setInt('counter', counter); }