-
-
Save ericgrandt/0553aa7f34798154b4c3401a9892150f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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