flutter-working-with-databases by flutter/skills
npx skills add https://github.com/flutter/skills --skill flutter-working-with-databases将数据层构建为所有应用程序数据的单一可信源。在 MVVM 架构中,数据层代表模型。切勿在此层之外更新应用程序数据。
将数据层分为两个不同的组件:仓库和服务。
Result 包装器。使用数据库在本地持久化和查询大量结构化数据。
sqflite 和 path 包添加到 pubspec.yaml。广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
path 包安全地定义跨平台的磁盘存储位置。id 作为主键并启用 AUTOINCREMENT 以提高查询和更新时间。whereArgs 以防止 SQL 注入。在仓库内结合本地和远程数据源,以提供无缝的离线支持。
Stream,该流立即从数据库服务产生缓存的本地数据,通过 API 服务执行网络请求,更新数据库服务,然后产生新数据。synchronized: false 并排队后台同步任务。根据数据负载选择合适的缓存策略:
shared_preferences 存储简单的应用配置、主题设置或用户偏好。cached_network_image 包将远程图像自动缓存到设备的文件系统。向应用程序添加新数据实体时,复制并跟踪此清单。
按照此顺序添加新的 SQLite 表并集成它。
sqflite 和 path 依赖项。onCreate 或 onUpgrade 方法以执行 CREATE TABLE 语句。insert、query、update 和 delete 方法。database.open()。此示例演示了一个仓库使用 Stream 在数据库服务和 API 服务之间协调,实现离线优先读取。
import 'dart:async';
class TodoRepository {
TodoRepository({
required DatabaseService databaseService,
required ApiClientService apiClientService,
}) : _databaseService = databaseService,
_apiClientService = apiClientService;
final DatabaseService _databaseService;
final ApiClientService _apiClientService;
/// Yields local data immediately, then fetches remote data, updates local, and yields fresh data.
Stream<List<Todo>> observeTodos() async* {
// 1. Yield local cached data first
final localTodos = await _databaseService.getAllTodos();
if (localTodos.isNotEmpty) {
yield localTodos.map((model) => Todo.fromDbModel(model)).toList();
}
try {
// 2. Fetch fresh data from API
final remoteTodos = await _apiClientService.fetchTodos();
// 3. Update local database
await _databaseService.replaceAllTodos(remoteTodos);
// 4. Yield fresh data
yield remoteTodos.map((model) => Todo.fromApiModel(model)).toList();
} on Exception catch (e) {
// Handle network errors (UI will still have local data)
// Log error or yield a specific error state if required
}
}
/// Offline-first write: Save locally, then attempt remote sync.
Future<void> createTodo(Todo todo) async {
final dbModel = todo.toDbModel().copyWith(isSynced: false);
// 1. Save locally immediately
await _databaseService.insertTodo(dbModel);
try {
// 2. Attempt remote sync
final apiModel = await _apiClientService.postTodo(todo.toApiModel());
// 3. Mark as synced locally
await _databaseService.updateTodo(
dbModel.copyWith(id: apiModel.id, isSynced: true)
);
} on Exception catch (_) {
// Leave as isSynced: false for background sync task to pick up later
}
}
}
演示使用 whereArgs 的安全查询构造。
class DatabaseService {
static const String _tableName = 'todos';
static const String _colId = 'id';
static const String _colTask = 'task';
static const String _colIsSynced = 'is_synced';
Database? _database;
Future<void> open() async {
if (_database != null) return;
final dbPath = join(await getDatabasesPath(), 'app_database.db');
_database = await openDatabase(
dbPath,
version: 1,
onCreate: (db, version) {
return db.execute(
'CREATE TABLE $_tableName('
'$_colId INTEGER PRIMARY KEY AUTOINCREMENT, '
'$_colTask TEXT, '
'$_colIsSynced INTEGER)'
);
},
);
}
Future<void> updateTodo(TodoDbModel todo) async {
await _database!.update(
_tableName,
todo.toMap(),
where: '$_colId = ?',
whereArgs: [todo.id], // Prevents SQL injection
);
}
}
每周安装量
1.8K
仓库
GitHub 星标数
792
首次出现
12 天前
安全审计
安装于
codex1.8K
gemini-cli1.8K
opencode1.8K
github-copilot1.8K
kimi-cli1.8K
cursor1.8K
Construct the data layer as the Single Source of Truth (SSOT) for all application data. In an MVVM architecture, the data layer represents the Model. Never update application data outside of this layer.
Separate the data layer into two distinct components: Repositories and Services.
Result wrappers to the calling repository.Use databases to persist and query large amounts of structured data locally.
sqflite and path packages to pubspec.yaml.path package to define the storage location on disk safely across platforms.id as the primary key with AUTOINCREMENT to improve query and update times.whereArgs in SQL queries to prevent SQL injection (e.g., where: 'id = ?', whereArgs: [id]).http package) in dedicated client classes.Future or Stream).freezed or built_value) for Domain Models.Combine local and remote data sources within the repository to provide seamless offline support.
Stream that immediately yields the cached local data from the Database Service, performs the network request via the API Service, updates the Database Service, and then yields the fresh data.synchronized: false and queue a background synchronization task.Select the appropriate caching strategy based on the data payload:
shared_preferences for simple app configurations, theme settings, or user preferences.sqflite, drift) or non-relational (hive_ce, isar_community) on-device databases.cached_network_image package to automatically cache remote images to the device's file system.Copy and track this checklist when adding a new data entity to the application.
Follow this sequence to add a new SQLite table and integrate it.
sqflite and path dependencies.onCreate or onUpgrade method in the Database Service to execute the CREATE TABLE statement.insert, query, update, and delete methods in the Database Service.database.open() before executing queries.This example demonstrates a Repository coordinating between a Database Service and an API Service using a Stream for offline-first reads.
import 'dart:async';
class TodoRepository {
TodoRepository({
required DatabaseService databaseService,
required ApiClientService apiClientService,
}) : _databaseService = databaseService,
_apiClientService = apiClientService;
final DatabaseService _databaseService;
final ApiClientService _apiClientService;
/// Yields local data immediately, then fetches remote data, updates local, and yields fresh data.
Stream<List<Todo>> observeTodos() async* {
// 1. Yield local cached data first
final localTodos = await _databaseService.getAllTodos();
if (localTodos.isNotEmpty) {
yield localTodos.map((model) => Todo.fromDbModel(model)).toList();
}
try {
// 2. Fetch fresh data from API
final remoteTodos = await _apiClientService.fetchTodos();
// 3. Update local database
await _databaseService.replaceAllTodos(remoteTodos);
// 4. Yield fresh data
yield remoteTodos.map((model) => Todo.fromApiModel(model)).toList();
} on Exception catch (e) {
// Handle network errors (UI will still have local data)
// Log error or yield a specific error state if required
}
}
/// Offline-first write: Save locally, then attempt remote sync.
Future<void> createTodo(Todo todo) async {
final dbModel = todo.toDbModel().copyWith(isSynced: false);
// 1. Save locally immediately
await _databaseService.insertTodo(dbModel);
try {
// 2. Attempt remote sync
final apiModel = await _apiClientService.postTodo(todo.toApiModel());
// 3. Mark as synced locally
await _databaseService.updateTodo(
dbModel.copyWith(id: apiModel.id, isSynced: true)
);
} on Exception catch (_) {
// Leave as isSynced: false for background sync task to pick up later
}
}
}
Demonstrates safe query construction using whereArgs.
class DatabaseService {
static const String _tableName = 'todos';
static const String _colId = 'id';
static const String _colTask = 'task';
static const String _colIsSynced = 'is_synced';
Database? _database;
Future<void> open() async {
if (_database != null) return;
final dbPath = join(await getDatabasesPath(), 'app_database.db');
_database = await openDatabase(
dbPath,
version: 1,
onCreate: (db, version) {
return db.execute(
'CREATE TABLE $_tableName('
'$_colId INTEGER PRIMARY KEY AUTOINCREMENT, '
'$_colTask TEXT, '
'$_colIsSynced INTEGER)'
);
},
);
}
Future<void> updateTodo(TodoDbModel todo) async {
await _database!.update(
_tableName,
todo.toMap(),
where: '$_colId = ?',
whereArgs: [todo.id], // Prevents SQL injection
);
}
}
Weekly Installs
1.8K
Repository
GitHub Stars
792
First Seen
12 days ago
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex1.8K
gemini-cli1.8K
opencode1.8K
github-copilot1.8K
kimi-cli1.8K
cursor1.8K
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装