Flutter 本地存储
2023-02-26 18:58:40

Store key-value data on disk

Before starting, add the shared_preferences plugin to the pubspec.yaml file:

// 添加依赖
dependencies:
  flutter:
    sdk: flutter
  shared_preferences: "<newest version>"
// 保存数据
// obtain shared preferences
final prefs = await SharedPreferences.getInstance();
// set value
prefs.setInt('counter', counter);

// 读取数据
// Try reading data from the counter key. If it doesn't exist, return 0.
final counter = prefs.getInt('counter') ?? 0;

// 删除数据
prefs.remove('counter');