Skip to content

Instantly share code, notes, and snippets.

@ericgrandt
Last active May 6, 2019 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericgrandt/0553aa7f34798154b4c3401a9892150f to your computer and use it in GitHub Desktop.
Save ericgrandt/0553aa7f34798154b4c3401a9892150f to your computer and use it in GitHub Desktop.
class DBProvider {
// Singleton
DBProvider._();
// Static object to provide us access from practically anywhere
static final DBProvider db = DBProvider._();
Database _database;
Future<Database> get database async {
if (_database != null) {
return _database;
}
_database = await initDB();
return _database;
}
initDB() async {
// Retrieve your app's directory, then create a path to a database for your app.
Directory documentsDir = await getApplicationDocumentsDirectory();
String path = join(documentsDir.path, 'money_clip.db');
return await openDatabase(path, version: 1, onOpen: (db) async {
// Do something when the database is opened
}, onCreate: (Database db, int version) async {
// Do something, such as creating tables, when the database is first created.
// If the database already exists, this will not be called.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment