From a3c4bdb57f61f8445b7244e9c5a81acbc6f7a5c2 Mon Sep 17 00:00:00 2001 From: Subhranil Sarkar <57196419+subhranil2605@users.noreply.github.com> Date: Tue, 19 Oct 2021 00:23:45 +0530 Subject: [PATCH] Adding null safety --- lib/utils/database_helper.dart | 203 ++++++++++++++++----------------- 1 file changed, 96 insertions(+), 107 deletions(-) diff --git a/lib/utils/database_helper.dart b/lib/utils/database_helper.dart index 181c345..0491c28 100644 --- a/lib/utils/database_helper.dart +++ b/lib/utils/database_helper.dart @@ -1,113 +1,102 @@ import 'package:sqflite/sqflite.dart'; +import 'package:path_provider/path_provider.dart'; import 'dart:async'; import 'dart:io'; -import 'package:path_provider/path_provider.dart'; -import 'package:flutter_app/models/note.dart'; +import 'package:flutter_codes/models/note.dart'; class DatabaseHelper { - - static DatabaseHelper _databaseHelper; // Singleton DatabaseHelper - static Database _database; // Singleton Database - - String noteTable = 'note_table'; - String colId = 'id'; - String colTitle = 'title'; - String colDescription = 'description'; - String colPriority = 'priority'; - String colDate = 'date'; - - DatabaseHelper._createInstance(); // Named constructor to create instance of DatabaseHelper - - factory DatabaseHelper() { - - if (_databaseHelper == null) { - _databaseHelper = DatabaseHelper._createInstance(); // This is executed only once, singleton object - } - return _databaseHelper; - } - - Future get database async { - - if (_database == null) { - _database = await initializeDatabase(); - } - return _database; - } - - Future initializeDatabase() async { - // Get the directory path for both Android and iOS to store database. - Directory directory = await getApplicationDocumentsDirectory(); - String path = directory.path + 'notes.db'; - - // Open/create the database at a given path - var notesDatabase = await openDatabase(path, version: 1, onCreate: _createDb); - return notesDatabase; - } - - void _createDb(Database db, int newVersion) async { - - await db.execute('CREATE TABLE $noteTable($colId INTEGER PRIMARY KEY AUTOINCREMENT, $colTitle TEXT, ' - '$colDescription TEXT, $colPriority INTEGER, $colDate TEXT)'); - } - - // Fetch Operation: Get all note objects from database - Future>> getNoteMapList() async { - Database db = await this.database; - -// var result = await db.rawQuery('SELECT * FROM $noteTable order by $colPriority ASC'); - var result = await db.query(noteTable, orderBy: '$colPriority ASC'); - return result; - } - - // Insert Operation: Insert a Note object to database - Future insertNote(Note note) async { - Database db = await this.database; - var result = await db.insert(noteTable, note.toMap()); - return result; - } - - // Update Operation: Update a Note object and save it to database - Future updateNote(Note note) async { - var db = await this.database; - var result = await db.update(noteTable, note.toMap(), where: '$colId = ?', whereArgs: [note.id]); - return result; - } - - // Delete Operation: Delete a Note object from database - Future deleteNote(int id) async { - var db = await this.database; - int result = await db.rawDelete('DELETE FROM $noteTable WHERE $colId = $id'); - return result; - } - - // Get number of Note objects in database - Future getCount() async { - Database db = await this.database; - List> x = await db.rawQuery('SELECT COUNT (*) from $noteTable'); - int result = Sqflite.firstIntValue(x); - return result; - } - - // Get the 'Map List' [ List ] and convert it to 'Note List' [ List ] - Future> getNoteList() async { - - var noteMapList = await getNoteMapList(); // Get 'Map List' from database - int count = noteMapList.length; // Count the number of map entries in db table - - List noteList = List(); - // For loop to create a 'Note List' from a 'Map List' - for (int i = 0; i < count; i++) { - noteList.add(Note.fromMapObject(noteMapList[i])); - } - - return noteList; - } - + static DatabaseHelper? _databaseHelper; + static Database? _database; + + String noteTable = 'note_table'; + String colId = 'id'; + String colTitle = 'title'; + String colDescription = 'description'; + String colPriority = 'priority'; + String colDate = 'date'; + + DatabaseHelper._createInstance(); + + factory DatabaseHelper() { + if (_databaseHelper == null) { + _databaseHelper = DatabaseHelper._createInstance(); + } + return _databaseHelper!; + } + + Future get database async { + if (_database == null) { + _database = await initializeDatabase(); + } + return _database!; + } + + Future initializeDatabase() async { + Directory directory = await getApplicationDocumentsDirectory(); + String path = directory.path + 'notes.db'; + + var notesDatabase = await openDatabase( + path, + version: 1, + onCreate: _createDb, + ); + return notesDatabase; + } + + void _createDb(Database db, int newVersion) async { + await db.execute( + 'CREATE TABLE $noteTable($colId INTEGER PRIMARY KEY AUTOINCREMENT, $colTitle TEXT, ' + '$colDescription TEXT, $colPriority INTEGER, $colDate TEXT)'); + } + + // Fetch Operation + Future>> getNoteMapList() async { + Database db = await this.database; + var result = await db.query(noteTable, orderBy: '$colPriority ASC'); + return result; + } + + // Insert + Future insertNote(Note note) async { + Database db = await this.database; + var result = await db.insert(noteTable, note.toMap()); + return result; + } + + // Update + Future updateNote(Note note) async { + Database db = await this.database; + var result = await db.update(noteTable, note.toMap(), + where: '$colId = ?', whereArgs: [note.id]); + return result; + } + + // Delete + Future deleteNote(int id) async { + Database db = await this.database; + var result = + await db.rawDelete('DELETE FROM $noteTable WHERE $colId = $id'); + return result; + } + + // count + Future getCount() async { + Database db = await this.database; + List> x = + await db.rawQuery('SELECT COUNT (*) FROM $noteTable'); + int? result = Sqflite.firstIntValue(x); + return result!; + } + + Future> getNoteList() async { + var noteMapList = await getNoteMapList(); + int count = noteMapList.length; + + List noteList = []; + for (int i = 0; i < count; i++) { + noteList.add(Note.fromMapObject(noteMapList[i])); + } + + return noteList; + } } - - - - - - -