存储
简介
Nylo Website v7 通过 NyStorage 类提供强大的存储系统。它底层使用 flutter_secure_storage 在用户设备上安全存储数据。
import 'package:nylo_framework/nylo_framework.dart';
// Save a value
await NyStorage.save("coins", 100);
// Read a value
int? coins = await NyStorage.read<int>('coins'); // 100
// Delete a value
await NyStorage.delete('coins');
保存值
使用 NyStorage.save() 或 storageSave() 辅助函数保存值:
// Using NyStorage class
await NyStorage.save("username", "Anthony");
await NyStorage.save("score", 1500);
await NyStorage.save("isPremium", true);
// Using helper function
await storageSave("username", "Anthony");
同时保存到 Backpack
同时存储到持久存储和内存中的 Backpack:
await NyStorage.save('auth_token', 'abc123', inBackpack: true);
// Now accessible synchronously via Backpack
String? token = Backpack.instance.read('auth_token');
读取值
使用自动类型转换读取值:
// Read as String (default)
String? username = await NyStorage.read('username');
// Read with type casting
int? score = await NyStorage.read<int>('score');
double? rating = await NyStorage.read<double>('rating');
bool? isPremium = await NyStorage.read<bool>('isPremium');
// With default value
String name = await NyStorage.read('name', defaultValue: 'Guest') ?? 'Guest';
// Using helper function
String? username = await storageRead('username');
int? score = await storageRead<int>('score');
删除值
// Delete a single key
await NyStorage.delete('username');
// Delete and remove from Backpack too
await NyStorage.delete('auth_token', andFromBackpack: true);
// Delete multiple keys
await NyStorage.deleteMultiple(['key1', 'key2', 'key3']);
// Delete all (with optional exclusions)
await NyStorage.deleteAll();
await NyStorage.deleteAll(excludeKeys: ['auth_token']);
存储键
在 lib/config/storage_keys.dart 中组织您的存储键:
final class StorageKeysConfig {
// Auth key for user authentication
static StorageKey auth = 'SK_AUTH';
// Keys synced on app boot
static syncedOnBoot() => () async {
return [
coins.defaultValue(0),
themePreference.defaultValue('light'),
];
};
static StorageKey coins = 'SK_COINS';
static StorageKey themePreference = 'SK_THEME_PREFERENCE';
static StorageKey onboardingComplete = 'SK_ONBOARDING_COMPLETE';
}
使用 StorageKey 扩展
StorageKey 是 String 的类型别名,附带一组强大的扩展方法:
// Save
await StorageKeysConfig.coins.save(100);
// Save with Backpack
await StorageKeysConfig.coins.save(100, inBackpack: true);
// Read
int? coins = await StorageKeysConfig.coins.read<int>();
// Read with default value
int? coins = await StorageKeysConfig.coins.fromStorage<int>(defaultValue: 0);
// Save/Read JSON
await StorageKeysConfig.coins.saveJson({"gold": 50, "silver": 200});
Map? data = await StorageKeysConfig.coins.readJson<Map>();
// Delete
await StorageKeysConfig.coins.deleteFromStorage();
// Delete (alias)
await StorageKeysConfig.coins.flush();
// Read from Backpack (synchronous)
int? coins = StorageKeysConfig.coins.fromBackpack<int>();
// Collection operations
await StorageKeysConfig.coins.addToCollection<int>(100);
List<int> allCoins = await StorageKeysConfig.coins.readCollection<int>();
保存/读取 JSON
存储和检索 JSON 数据:
// Save JSON
Map<String, dynamic> user = {
"name": "Anthony",
"email": "anthony@example.com",
"preferences": {"theme": "dark"}
};
await NyStorage.saveJson("user_data", user);
// Read JSON
Map<String, dynamic>? userData = await NyStorage.readJson("user_data");
print(userData?['name']); // "Anthony"
TTL(生存时间)
Nylo Website v7 支持存储带有自动过期功能的值:
// Save with 1 hour expiration
await NyStorage.saveWithExpiry(
'session_token',
'abc123',
ttl: Duration(hours: 1),
);
// Read (returns null if expired)
String? token = await NyStorage.readWithExpiry<String>('session_token');
// Check remaining time
Duration? remaining = await NyStorage.getTimeToLive('session_token');
if (remaining != null) {
print('Expires in ${remaining.inMinutes} minutes');
}
// Clean up all expired keys
int removed = await NyStorage.removeExpired();
print('Removed $removed expired keys');
批量操作
高效处理多个存储操作:
// Save multiple values
await NyStorage.saveAll({
'username': 'Anthony',
'score': 1500,
'level': 10,
});
// Read multiple values
Map<String, dynamic?> values = await NyStorage.readMultiple<dynamic>([
'username',
'score',
'level',
]);
// Delete multiple keys
await NyStorage.deleteMultiple(['temp_1', 'temp_2', 'temp_3']);
集合简介
集合允许您在单个键下存储项目列表:
// Add items to a collection
await NyStorage.addToCollection("favorites", item: "Product A");
await NyStorage.addToCollection("favorites", item: "Product B");
// Read the collection
List<String> favorites = await NyStorage.readCollection<String>("favorites");
// ["Product A", "Product B"]
添加到集合
// Add item (allows duplicates by default)
await NyStorage.addToCollection("cart_ids", item: 123);
// Prevent duplicates
await NyStorage.addToCollection(
"cart_ids",
item: 123,
allowDuplicates: false,
);
// Save entire collection at once
await NyStorage.saveCollection<int>("cart_ids", [1, 2, 3, 4, 5]);
读取集合
// Read collection with type
List<int> cartIds = await NyStorage.readCollection<int>("cart_ids");
// Check if collection is empty
bool isEmpty = await NyStorage.isCollectionEmpty("cart_ids");
更新集合
// Update item by index
await NyStorage.updateCollectionByIndex<int>(
0, // index
(item) => item + 10, // transform function
key: "scores",
);
// Update items matching a condition
await NyStorage.updateCollectionWhere<Map<String, dynamic>>(
(item) => item['id'] == 5, // where condition
key: "products",
update: (item) {
item['quantity'] = item['quantity'] + 1;
return item;
},
);
从集合中删除
// Delete by index
await NyStorage.deleteFromCollection<String>(0, key: "favorites");
// Delete by value
await NyStorage.deleteValueFromCollection<int>(
"cart_ids",
value: 123,
);
// Delete items matching a condition
await NyStorage.deleteFromCollectionWhere<Map<String, dynamic>>(
(item) => item['expired'] == true,
key: "coupons",
);
// Delete entire collection
await NyStorage.delete("favorites");
Backpack 存储
Backpack 是一个轻量级的内存存储类,用于在用户会话期间进行快速同步访问。数据在应用关闭时不会持久化。
保存到 Backpack
// Using helper
backpackSave('user_token', 'abc123');
backpackSave('user', userObject);
// Using Backpack directly
Backpack.instance.save('settings', {'darkMode': true});
从 Backpack 读取
// Using helper
String? token = backpackRead('user_token');
User? user = backpackRead<User>('user');
// Using Backpack directly
var settings = Backpack.instance.read('settings');
从 Backpack 删除
backpackDelete('user_token');
// Delete all
backpackDeleteAll();
实际示例
// After login, store token in both persistent and session storage
Future<void> handleLogin(String token) async {
// Persist for app restarts
await NyStorage.save('auth_token', token);
// Also store in Backpack for quick access
backpackSave('auth_token', token);
}
// In API service, access synchronously
class ApiService extends NyApiService {
Future<User?> getProfile() async {
return await network<User>(
request: (request) => request.get("/profile"),
bearerToken: backpackRead('auth_token'), // No await needed
);
}
}
使用 Backpack 持久化
一次调用同时存储到持久存储和 Backpack:
// Save to both
await NyStorage.save('user_token', 'abc123', inBackpack: true);
// Now accessible via Backpack (sync) and NyStorage (async)
String? tokenSync = Backpack.instance.read('user_token');
String? tokenAsync = await NyStorage.read('user_token');
将存储同步到 Backpack
在应用启动时将所有持久存储加载到 Backpack:
// In your app provider
await NyStorage.syncToBackpack();
// With overwrite option
await NyStorage.syncToBackpack(overwrite: true);
会话
会话提供命名的内存存储,用于分组相关数据(不持久化):
// Create/access a session and add data
session('checkout')
.add('items', ['Product A', 'Product B'])
.add('total', 99.99)
.add('coupon', 'SAVE10');
// Or initialize with data
session('checkout', {
'items': ['Product A', 'Product B'],
'total': 99.99,
});
// Read session data
List<String>? items = session('checkout').get<List<String>>('items');
double? total = session('checkout').get<double>('total');
// Get all session data
Map<String, dynamic>? checkoutData = session('checkout').data();
// Delete a single value
session('checkout').delete('coupon');
// Clear entire session
session('checkout').clear();
// or
session('checkout').flush();
持久化会话
会话可以同步到持久存储:
// Save session to storage
await session('checkout').syncToStorage();
// Restore session from storage
await session('checkout').syncFromStorage();
会话使用场景
会话非常适用于:
- 多步骤表单(引导流程、结账)
- 临时用户偏好设置
- 向导/旅程流程
- 购物车数据
Model 保存
Model 基类提供内置的存储方法。当您在构造函数中定义 key 时,模型可以自行保存:
class User extends Model {
String? name;
String? email;
// Define a storage key
static StorageKey key = 'user';
User() : super(key: key);
User.fromJson(dynamic data) : super(key: key) {
name = data['name'];
email = data['email'];
}
@override
Map<String, dynamic> toJson() => {
"name": name,
"email": email,
};
}
保存 Model
User user = User();
user.name = "Anthony";
user.email = "anthony@example.com";
// Save to persistent storage
await user.save();
// Save to both storage and Backpack
await user.save(inBackpack: true);
读取 Model
User? user = await NyStorage.read<User>(User.key);
同步到 Backpack
将模型从存储加载到 Backpack 以进行同步访问:
bool found = await User().syncToBackpack();
if (found) {
User? user = Backpack.instance.read<User>(User.key);
}
Model 集合
将模型保存到集合:
User userAnthony = User();
userAnthony.name = 'Anthony';
await userAnthony.saveToCollection();
User userKyle = User();
userKyle.name = 'Kyle';
await userKyle.saveToCollection();
// Read back
List<User> users = await NyStorage.readCollection<User>(User.key);
StorageKey 扩展参考
StorageKey 是 String 的类型别名。NyStorageKeyExt 扩展提供以下方法:
| 方法 | 返回值 | 描述 |
|---|---|---|
save(value, {inBackpack}) |
Future |
将值保存到存储 |
saveJson(value, {inBackpack}) |
Future |
将 JSON 值保存到存储 |
read<T>({defaultValue}) |
Future<T?> |
从存储中读取值 |
readJson<T>({defaultValue}) |
Future<T?> |
从存储中读取 JSON 值 |
fromStorage<T>({defaultValue}) |
Future<T?> |
read 的别名 |
fromBackpack<T>({defaultValue}) |
T? |
从 Backpack 读取(同步) |
toModel<T>() |
T |
将 JSON 字符串转换为模型 |
addToCollection<T>(value, {allowDuplicates}) |
Future |
将项目添加到集合 |
readCollection<T>() |
Future<List<T>> |
读取集合 |
deleteFromStorage({andFromBackpack}) |
Future |
从存储中删除 |
flush({andFromBackpack}) |
Future |
deleteFromStorage 的别名 |
defaultValue<T>(value) |
Future Function(bool)? |
当键为空时设置默认值(用于 syncedOnBoot) |
存储异常
Nylo Website v7 提供类型化的存储异常:
| 异常 | 描述 |
|---|---|
StorageException |
基础异常,包含消息和可选键 |
StorageSerializationException |
序列化对象到存储失败 |
StorageDeserializationException |
反序列化存储数据失败 |
StorageKeyNotFoundException |
存储键未找到 |
StorageTimeoutException |
存储操作超时 |
try {
await NyStorage.read<User>('user');
} on StorageDeserializationException catch (e) {
print('Failed to load user: ${e.message}');
print('Expected type: ${e.expectedType}');
}
旧版迁移
Nylo Website v7 使用新的信封存储格式。如果您从 v6 升级,可以迁移旧数据:
// Call during app initialization
int migrated = await NyStorage.migrateToEnvelopeFormat();
print('Migrated $migrated keys to new format');
这会将旧版格式(独立的 _runtime_type 键)转换为新的信封格式。迁移可以安全地多次运行——已迁移的键将被跳过。