brick_sqlite 4.0.2
brick_sqlite: ^4.0.2 copied to clipboard
SQLite connector for Brick, a data persistence library for Flutter
import 'package:brick_core/core.dart';
import 'package:brick_sqlite/brick_sqlite.dart';
import 'package:sqflite_common/sqlite_api.dart';
// Use sqflite when building for Flutter
// import 'package:sqflite/sqflite.dart' show databaseFactory;
import 'package:sqflite_common_ffi/sqflite_ffi.dart' show databaseFactory;
/// This class and code is always generated.
/// It is included here as an illustration.
/// Sqlite adapters are generated by domains that utilize the brick_sqlite_generators package,
/// such as brick_offline_first_with_rest_build
class UserAdapter extends SqliteAdapter<User> {
@override
final tableName = 'User';
@override
final fieldsToSqliteColumns = {
'primaryKey': const RuntimeSqliteColumnDefinition(
columnName: '_brick_id',
type: int,
),
'name': const RuntimeSqliteColumnDefinition(
columnName: 'name',
type: String,
),
};
@override
Future<int?> primaryKeyByUniqueColumns(User instance, DatabaseExecutor executor) async =>
instance.primaryKey;
@override
Future<User> fromSqlite(
Map<String, dynamic> data, {
required SqliteProvider provider,
ModelRepository<SqliteModel>? repository,
}) async {
return User(
name: data['name'],
);
}
@override
Future<Map<String, dynamic>> toSqlite(
User instance, {
required SqliteProvider provider,
ModelRepository<SqliteModel>? repository,
}) async {
return {
'name': instance.name,
};
}
}
/// This value is always generated.
/// It is included here as an illustration.
/// Import it from `lib/brick/brick.g.dart` in your application.
final dictionary = SqliteModelDictionary({
User: UserAdapter(),
});
/// A model is unique to the end implementation (e.g. a Flutter app)
class User extends SqliteModel {
final String? name;
User({
this.name,
});
}
class MyRepository extends SingleProviderRepository<SqliteModel> {
MyRepository()
: super(
SqliteProvider(
'myApp.sqlite',
databaseFactory: databaseFactory,
modelDictionary: dictionary,
),
);
}
void main() async {
final repository = MyRepository();
final users = await repository.get<User>();
// ignore: avoid_print
print(users);
}