Skip to content

Instantly share code, notes, and snippets.

@ericgrandt
Created April 17, 2019 18:50
Show Gist options
  • Save ericgrandt/f6835ec9f36832eb7440a6ab1a4443a7 to your computer and use it in GitHub Desktop.
Save ericgrandt/f6835ec9f36832eb7440a6ab1a4443a7 to your computer and use it in GitHub Desktop.
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
class DBProvider {
// Create a singleton
DBProvider._();
static final DBProvider db = DBProvider._();
Database _database;
Future<Database> get database async {
if (_database != null) {
return _database;
}
_database = await initDB();
return _database;
}
initDB() async {
// Get the location of our app directory. This is where files for our app,
// and only our app, are stored. Files in this directory are deleted
// when the app is deleted.
Directory documentsDir = await getApplicationDocumentsDirectory();
String path = join(documentsDir.path, 'app.db');
return await openDatabase(path, version: 1, onOpen: (db) async {
}, onCreate: (Database db, int version) async {
// Create the note table
await db.execute('''
CREATE TABLE note(
id INTEGER PRIMARY KEY,
contents TEXT DEFAULT ''
)
''');
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment