From c5f5a8654cd41749cc92d49063d68ab80c7f471c Mon Sep 17 00:00:00 2001 From: SomnusVeritas Date: Wed, 8 Nov 2023 10:39:31 +0100 Subject: [PATCH] simple dbhelper with Isar --- lib/main.dart | 4 +++- lib/models/todo.dart | 13 ++++++++++--- lib/services/dbhelper.dart | 17 ++++++++++++++++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 72ff1f1..b52148f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,10 @@ import 'package:flutter/material.dart'; import 'pages/dashboard_page.dart'; +import 'services/dbhelper.dart'; -void main() { +void main() async { + await DbHelper.init(); runApp(const MyApp()); } diff --git a/lib/models/todo.dart b/lib/models/todo.dart index 95db1d0..59b10fd 100644 --- a/lib/models/todo.dart +++ b/lib/models/todo.dart @@ -1,11 +1,18 @@ +import 'package:isar/isar.dart'; + +part 'todo.g.dart'; + +@collection class Todo { + final int id; final String title; final String description; final DateTime createdAt; Todo({ + required this.id, required this.title, - required this.description, - required this.createdAt, - }); + this.description = '', + DateTime? createdAt, + }) : createdAt = createdAt ?? DateTime.now(); } diff --git a/lib/services/dbhelper.dart b/lib/services/dbhelper.dart index be01102..5ee9cf5 100644 --- a/lib/services/dbhelper.dart +++ b/lib/services/dbhelper.dart @@ -1 +1,16 @@ -class DbHelper {} +import 'package:path_provider/path_provider.dart'; +import 'package:isar/isar.dart'; + +import '../models/todo.dart'; + +class DbHelper { + static late Isar _isar; + + static init() async { + final dir = await getApplicationDocumentsDirectory(); + _isar = Isar.open( + schemas: [TodoSchema], + directory: dir.path, + ); + } +}