0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Flutter : SharedPrefences】モバイル端末にデータを保存・取得する方法

Posted at

SharedPrefences

モバイル端末にデータを保存・取得するには、「SharedPrefences」を使用します。
まずは、プロジェクトにインストール
最新版は、こちらから SharedPrefences

pubsepc.yaml
dependencies:
 flutter:
  sdk: flutter
 //以下を追加
 shared_preferences: ^2.0.6

set

  • [キーの名前]をキーにして、データを保存
SharedPreferences prefs = await SharedPreferences.getInstance();
int counter = 1;
await prefs.setInt('[キーの名前]', counter);

get

-[キーの名前]からデータを取得
-[キーの名前]にデータが保存されていなかったら、??の後ろを代入

SharedPreferences prefs = await SharedPreferences.getInstance();
counter = prefs.getInt('[キーの名前]') ?? 0

型ごとの取得と保存

Int Double String bool List
取得 getInt getDouble getString getBool getList
保存 setInt setDouble setString setBool setList
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?