Skip to content

Instantly share code, notes, and snippets.

@Bruising6802
Last active May 23, 2020 05:09
Show Gist options
  • Save Bruising6802/05737db703c13dcf98e33803c578d07d to your computer and use it in GitHub Desktop.
Save Bruising6802/05737db703c13dcf98e33803c578d07d to your computer and use it in GitHub Desktop.
A database of sqflite in flutter for todos
import 'dart:async';
import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite/sqflite.dart';
import 'package:meta/meta.dart';
class ToDo {
@required
int id;
@required
String title;
@required
String description;
@required
int orderItems;
@required
bool complete;
@required
String datetime;
@required
int priorty;
ToDo(
{this.id,
this.title,
this.description,
this.orderItems,
this.complete,
this.datetime,
this.priorty,
this.subtasks});
ToDo.fromDb(Map<String, dynamic> map)
: id = map['id'],
title = map['title'],
description = map['description'],
orderItems = map['order_items'],
complete = map['complete'] == 1,
datetime = map['datetime'],
priorty = map['priorty'],
subtasks = map['subtasks'];
Map<String, dynamic> toMapForDb() {
var map = Map<String, dynamic>();
map['id'] = id;
map['title'] = title;
map['description'] = description;
map['order_items'] = orderItems;
map['complete'] = complete ? 1 : 0;
map['datetime'] = datetime;
map['priorty'] = priorty;
map['subtasks'] = subtasks;
return map;
}
}
class ToDoDatabase {
static final ToDoDatabase _instance = ToDoDatabase._();
static Database _database;
ToDoDatabase._();
factory ToDoDatabase() {
return _instance;
}
Future<Database> get db async {
if (_database != null) {
return _database;
}
_database = await initialize();
return _database;
}
Future<Database> initialize() async {
Directory directory = await getApplicationDocumentsDirectory();
String dbPath = join(directory.path, 'database.db');
var database = openDatabase(dbPath,
version: 1, onCreate: _onCreate, onUpgrade: _onUpgrade);
return database;
}
void _onCreate(Database db, int version) {
db.execute('''
CREATE TABLE todo(
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
description TEXT,
order_items INTEGER,
complete INTEGER,
datetime TEXT,
priorty INTEGER,
subtasks TEXT)
''');
print("Database was created!");
}
void _onUpgrade(Database db, int oldVersion, int newVersion) {
// Run migration according database versions
}
Future<int> addToDo(ToDo todo) async {
var client = await db;
return client.insert('todo', todo.toMapForDb(),
conflictAlgorithm: ConflictAlgorithm.replace);
}
Future<ToDo> fetchToDo(int id) async {
var client = await db;
final Future<List<Map<String, dynamic>>> futureMaps =
client.query('todo', where: 'id = ?', whereArgs: [id]);
var maps = await futureMaps;
if (maps.length != 0) {
return ToDo.fromDb(maps.first);
}
return null;
}
Future<List<ToDo>> fetchAll() async {
var client = await db;
var res = await client.query('todo');
if (res.isNotEmpty) {
List<ToDo> todo = res.map((todoMap) => ToDo.fromDb(todoMap)).toList();
return todo;
}
return [];
}
Future<int> updateToDo(ToDo newToDo) async {
var client = await db;
return client.update('todo', newToDo.toMapForDb(),
where: 'id = ?',
whereArgs: [newToDo.id],
conflictAlgorithm: ConflictAlgorithm.replace);
}
Future<void> removeToDo(int id) async {
var client = await db;
return client.delete('todo', where: 'id = ?', whereArgs: [id]);
}
Future<void> removeAll() async {
var client = await db;
return client.delete('todo');
}
Future dispose() async {
var client = await db;
client.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment