sqlite_handler 0.0.1
sqlite_handler: ^0.0.1 copied to clipboard
sqlite handler its package for handle models and database queries.
#Sqlite Handler A small and light package to deal with the Sqlite database by writing only the name of the database and its fields, and then calling the methods in the package in order to complete the database queries from creating, deleting, modifying and deleting.
Usage #
- first import package.
import 'package:sqlite_handler/sqlite_handler.dart';
- then make extend from Model class.
class TestModel extends Model{}
- then write table name and columns, columns must write in map with SqlTypes class for column data type
TestModel({this.id, this.name, this.createdAt})
: super('table_name_here', {
'id': SqlTypes.integer,
'name': SqlTypes.text,
'created_at': SqlTypes.text
});
List<TestModel> list = [];
void loadTasks() async {
/// load and add tasks to [tasks list]
TestModel().all().then((value) => list = value);
}
Create
void createTask() async {
TestModel testModel = TestModel(
name: "column value here", createdAt: DateTime.now().toString());
testModel.insert().then((val) {
print("Task was Created Successfully");
});
}
Update
void updateTask(int id, String name) async {
/// first get the task item
/// this step is must for Additional columns that is not send from user
Map? task = await TestModel().getItem(id);
/// insert getting item to fromMap method
TestModel testModel = TestModel().fromMap(task!);
/// then insert new to data to update
/// and in update method you should insert id
var result =
await TestModel(id: id, name: name, createdAt: testModel.createdAt)
.update(id);
if (result == 1) {
print("Task $name was updated");
} else {
print("Cannot update $name");
}
}
Delete
void deleteTask(int id, String? name) async {
int result = await TestModel().delete(id);
if (result == 1) {
print("Task $name was deleted");
} else {
print("Cannot delete $name");
}
}
Additional information #
to implement get all record you should insert this method in your model file
Future<List<TestModel>> all() async {
var maps = await get();
return List.generate(maps.length, (i) { return TestModel().fromMap(maps[i]); });
}